<?php

// 返回首页轮播基本信息
function home($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
		
	$user_location = trim(get_jsonValue($json,"user_location")) ;
	$addwh = " and  `area` = '' " ;
	if (strlen($user_location) > 0){	
		$add2 = "" ;
		$homes = sql_fetch_rows("select * from sys_banner_area where `area` = '$user_location'") ;
		if (is_array($homes) == true && count($homes) > 0){
			$idlist = "" ;  $dh = "" ;
			foreach ($homes as $h){
				$idlist .= $dh.$h['banner_id'] ;		$dh = "," ;
			}
			if (strlen($idlist) > 0){
				$add2 = " or `id` in ($idlist)" ;
			}
		}
		$addwh = " and (`area` = '$user_location' or `area` = ''  $add2) " ;
	}
	
	$ret = array() ;
	$ret['data'] = array() ;	
	$ret['data']['banner'] = array() ;
	
	$bitem = sql_fetch_rows("select * from sys_home_banner where `type` = 1 and `useing` = 1 $addwh order by `top_time` desc , `create_time` desc") ;
	foreach ($bitem as $b){
		
		$rn = array() ;
		util_set_BANNER_ITEM($rn , $b) ;
		$ret['data']['banner'][] = $rn ;		
	}
	
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}

// 返回首页推荐信息
function recommend($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}

	$user_location = get_jsonValue($json,"user_location") ;
	$addwh = " and  `area` = '' " ;
	if (strlen($user_location) > 0){
		$add2 = "" ;
		$homes = sql_fetch_rows("select * from sys_banner_area where `area` = '$user_location'") ;
		if (is_array($homes) == true && count($homes) > 0){
			$idlist = "" ;  $dh = "" ;
			foreach ($homes as $h){
				$idlist .= $dh.$h['banner_id'] ;		$dh = "," ;
			}
			if (strlen($idlist) > 0){
				$add2 = " or `id` in ($idlist)" ;
			}
		}
		$addwh = " and (`area` = '$user_location' or `area` = ''  $add2) " ;
	}
			
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['recommend'] = array() ;
		
	$bitem = sql_fetch_rows("select * from sys_home_banner where `type` = 2 and `useing` = 1 $addwh and `action` not in ('set' , 'item') order by `top_time` desc , `create_time` desc") ;
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_RECOMMEND_ITEM($rn , $b) ;
		$ret['data']['recommend'][] = $rn ;
	}

	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}


// 返回首页推荐信息
function recommend_set($json)
{
    // 获取缓冲数据
    $cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
    $ret = mem_getValues($cache_key) ;
    if ($ret != null){
        return $ret ;
    }

    $user_location = get_jsonValue($json,"user_location") ;
    $addwh = " and  `area` = '' " ;
    if (strlen($user_location) > 0){
        $add2 = "" ;
        $homes = sql_fetch_rows("select * from sys_banner_area where `area` = '$user_location'") ;
        if (is_array($homes) == true && count($homes) > 0){
            $idlist = "" ;  $dh = "" ;
            foreach ($homes as $h){
                $idlist .= $dh.$h['banner_id'] ;		$dh = "," ;
            }
            if (strlen($idlist) > 0){
                $add2 = " or `id` in ($idlist)" ;
            }
        }
        $addwh = " and (`area` = '$user_location' or `area` = ''  $add2) " ;
    }

    $ret = array() ;
    $ret['data'] = array() ;
    $ret['data']['recommend'] = array() ;

    $bitem = sql_fetch_rows("select * from sys_home_banner where `type` = 2 and `useing` = 1 $addwh and `action` in ('set' , 'item') order by `top_time` desc , `create_time` desc") ;
    foreach ($bitem as $b){
        $rn = array() ;
        util_set_RECOMMEND_ITEM($rn , $b) ;
        $ret['data']['recommend'][] = $rn ;
    }

    // 设置缓冲数据
    mem_setKey($cache_key, $ret) ;
    return $ret ;
}

// 套装信息
function setlist($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
	
	util_get_PAGINATION($json , $offset , $limit) ;
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	$room 	= get_jsonValue($filter , "room") ;			$addwhere .= _filter_room_set($room) ;		// 空间
	$style	= get_jsonValue($filter , "style") ;		$addwhere .= _filter_style_set($style) ;	// 风格
	$color 	= get_jsonValue($filter , "color") ;		$addwhere .= _filter_color_set($color) ;	// 色系
	$price	= get_jsonValue($filter , "price") ;		// 价格区间
	$price_min = get_jsonValue($price , "min") ;
	$price_max = get_jsonValue($price , "max") ;		$addwhere .= _filter_price_item(intval($price_min) , intval($price_max)) ;	// 价格区间
	$order_by = get_jsonValue($filter , "order_by") ;// 排序方式
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	$allcount = sql_fetch_one_cell("select count(sid) from taozhuang where `app_return` = 1 $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select * from taozhuang where `app_return` = 1 $addwhere order by `top_time` desc , `sid` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_SET_SIMPLE($rn , $b) ;
		$ret['data']['set'][] = $rn ;
	}
	
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
function setlist_v2($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
	
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	$room 	= get_jsonValue($filter , "room") ;			$addwhere .= _filter_room_set(intval($room)) ;		// 空间
	$style	= get_jsonValue($filter , "style") ;		$addwhere .= _filter_style_set(intval($style)) ;	// 风格
	$color 	= get_jsonValue($filter , "color") ;		$addwhere .= _filter_color_set(intval($color)) ;	// 色系
	$price	= get_jsonValue($filter , "price") ;		// 价格区间
	$price_min = get_jsonValue($price , "min") ;
	$price_max = get_jsonValue($price , "max") ;		$addwhere .= _filter_price_item(intval($price_min) , intval($price_max)) ;	// 价格区间
	$order_by = get_jsonValue($filter , "order_by") ;// 排序方式
	
	$brand  = intval(get_jsonValue($filter, "brand")) - 10 ;	// 品牌+类别

    $onebd = sql_fetch_one("select * from prf_tz_brand where id = '$brand'") ;
	if (is_array($onebd) && array_key_exists("name", $onebd)){
		$addwhere .= " and `state_tree` = $brand " ;
	}

	/*
	if ($brand > 0){
		if ($brand > 1000){
			$classid = intval($brand / 1000) * 1000 ;
			$brandid = $brand % 1000 ;
			$addwhere .= " and `sid` in (select p.tzid from taozhuang_pos p inner join sys_item s on p.item_id = s.id where s.class_id = $classid and s.brand_id = $brandid group by p.tzid)" ;
		}else{
			$brandid = $brand ;
			$addwhere .= " and `sid` in (select p.tzid from taozhuang_pos p left join sys_item s on p.item_id = s.id where s.brand_id = $brandid group by p.tzid)" ;
		}
	}
	*/
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(sid) from taozhuang where `app_return` = 1 $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	

	$bitem = sql_fetch_rows("select * from taozhuang where `app_return` = 1 $addwhere order by `top_time` desc , `sid` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_SET_SIMPLE($rn , $b) ;
		$ret['data']['set'][] = $rn ;
	}
	
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
// 套装详细信息
function set($json , $session)
{
	// 获取缓冲数据
	$uid = util_set_GETUID_for_SESSION($session) ;
	
	if ($uid <= 0){
		// 获取缓冲数据
		$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
		$ret = mem_getValues($cache_key) ;
		if ($ret != null){
			return $ret ;
		}
	}
	
	
	$id = get_jsonValue($json,"id") ;

	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	$item = sql_fetch_one("select * from taozhuang where `sid` = '$id'") ;
	if (is_null($item) || array_key_exists('sid' , $item) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NO_DATA"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NO_DATA"] ;
		return $ret ;
	}
	
	util_set_SET($ret['data']['set'] , $item , $uid) ;
	_util_save_user_log($uid , $ret['data']['set']['name'] , "set" , $id ,"") ;

	// 设置缓冲数据
	if ($uid <= 0){
		mem_setKey($cache_key, $ret) ;
	}
	return $ret ;
}
// 套装的换装信息
function set_item($json , $session)
{
	$id = get_jsonValue($json,"id") ;
	$uid = util_set_GETUID_for_SESSION($session) ;
	
	if ($uid <= 0){
		// 获取缓冲数据
		$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
		$ret = mem_getValues($cache_key) ;
		if ($ret != null){
			return $ret ;
		}
	}
	
	$ret = array() ;
	$ret['data'] = array() ;
	
	$item = sql_fetch_one("select * from taozhuang_pos  where `id` = '$id'");
	if (is_null($item) || array_key_exists('tzid' , $item) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NO_DATA"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NO_DATA"] ;
		return $ret ;
	}
	
	$ret['data']['list'] = array() ;
	$listtz = sql_fetch_rows("select * from taozhuang_pos  where `tzid` = '{$item['tzid']}' and `pos` = '{$item['pos']}' order by `order` asc");
	foreach($listtz as $tz){
		$v = array() ;		
		util_set_SET_POS_ITEM($v , $tz , $uid) ;
		$v['id'] = $id ;		
		$ret['data']['list'][] = $v ;
	}	
	
	if ($uid <= 0){
		mem_setKey($cache_key, $ret) ;
	}
	return $ret ;
}

// 单品信息
function itemlist($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
	
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	$room 	= get_jsonValue($filter , "room") ;			$addwhere .= _filter_room_item($room) ;		// 空间
	$style	= get_jsonValue($filter , "style") ;		$addwhere .= _filter_style_item($style) ;	// 风格
	$color 	= get_jsonValue($filter , "color") ;		$addwhere .= _filter_color_item($color) ;	// 色系
	$price	= get_jsonValue($filter , "price") ;		
	$price_min = get_jsonValue($price , "min") ;
	$price_max = get_jsonValue($price , "max") ;		$addwhere .= _filter_price_item(intval($price_min) , intval($price_max)) ;	// 价格区间
	$order_by = get_jsonValue($filter , "order_by") ;// 排序方式
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['item'] = array() ;
	
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(id) from `sys_item` where `app_return` = 1 $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select * from `sys_item` where `app_return` = 1 $addwhere order by `top_time` desc ,`id` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_ITEM_SIMPLE($rn , $b) ;	
		$ret['data']['item'][] = $rn ;
	}
	
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
function itemlist_v2($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
	
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	$class = get_jsonValue($filter , "category") ;		$addwhere .= _filter_class(intval($class)) ;		// 类别
	$brand = get_jsonValue($filter , "brand") ;			$addwhere .= _filter_brand_item(intval($brand)) ;	// 品牌
	$room 	= get_jsonValue($filter , "room") ;			$addwhere .= _filter_room_item(intval($room)) ;		// 空间
	$style	= get_jsonValue($filter , "style") ;		$addwhere .= _filter_style_item(intval($style)) ;	// 风格
	$color 	= get_jsonValue($filter , "color") ;		$addwhere .= _filter_color_item(intval($color)) ;	// 色系
	$price	= get_jsonValue($filter , "price") ;		
	$price_min = get_jsonValue($price , "min") ;
	$price_max = get_jsonValue($price , "max") ;		$addwhere .= _filter_price_item(intval($price_min) , intval($price_max)) ;	// 价格区间
	$order_by = get_jsonValue($filter , "order_by") ;// 排序方式
	
	$city_filter = trim(get_jsonValue($json,"city")) ;
	if (strlen($city_filter) >= 2){
		$brand_lists = array() ;
		$brand_lists = sql_fetch_rows("select b.* from grp_brand_city b inner join grp_city c on b.city_id = c.id inner join grp_brand d on b.brand_id = d.id and d.`useing` = 1 where c.`name` = '$city_filter'") ;
		$brands = "" ; $dh = "" ;
		foreach($brand_lists as $b){
			$brands .= $dh.$b['brand_id'] ; $dh = "," ;
		}
		//if ($brands != "")
		{
			$other_list = sql_fetch_rows("SELECT * from grp_brand a where  a.`useing` = 1 and  a.id not in (select e.brand_id from grp_brand_city e )") ;
			foreach($other_list as $b){
				$brands .= $dh.$b['id'] ; $dh = "," ;
			}

			$addwhere .= " and `brand_id` in ($brands) " ;	
		}
	}
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['item'] = array() ;
	
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(id) from `sys_item` where `app_return` = 1 $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select * from `sys_item` where `app_return` = 1 $addwhere order by `top_time` desc ,`id` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_ITEM_SIMPLE($rn , $b) ;	
		$ret['data']['item'][] = $rn ;
	}
	
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
function _filter_class($class_id)
{
	$addwhere = "" ;
	$class_id_a = 0 ;
	if ($class_id > 0){
		$i = 1 ;
		while($i <= 5){
			$v = pow(100, $i) ;
			if ($class_id % $v > 0){
				break ;
			}else{
				$class_id_a = $v - 1 ;
			}
			$i += 1 ;
		}
		$class_id_a = $class_id_a + $class_id ;
		$addwhere = " and `class_id` >= '$class_id' and `class_id` <= '$class_id_a' " ;
	}
	return $addwhere ;
}
function _filter_brand_item($brand)
{
	$addwhere = "" ;
	$rid = intval($brand) ;
	if ($rid == 0){
		$rid = intval(sql_fetch_one_cell("select `id` from grp_brand where `name` = '$brand'")) ;
	}
	if ($rid > 0){
		if ($rid < 10000){
			$addwhere = " and `brand_id` = '$rid' " ;
		}else{
			if ($rid % 10000 == 0){
				// 一组品牌
				$rid = $rid / 10000 ;
				$addwhere = " and `brand_id` in (select `id` from grp_brand where head_grp = '$rid' and `useing` = 1) " ;
			}else{
				// 某个品牌总的某个系列
				$seryid = intval($rid / 10000) ;	// 系列id
				$rid = intval($rid % 10000) ;		// 品牌id
				
				$addwhere = " and `brand_id` = '$rid' and `id` in (select `item_id` from sys_item_sery where `sery_id` = '$seryid') " ;
			}
		}
	}else{
		$addwhere = " and `brand_id` in (select `id` from grp_brand where `useing` = 1) " ;
	}
	return $addwhere ;
}
function _filter_room_item($room)
{
	$addwhere = "" ;
	$rid = intval($room) ;
	if ($rid == 0){
		$rid = intval(sql_fetch_one_cell("select `id` from grp_space where `name` = '$room'")) ;
	}
	if ($rid > 0){
		$addwhere = " and `id` in (select `item_id` from sys_item_space where `space_id` = '$rid')" ;
	}
	return $addwhere ;
}
function _filter_style_item($style)
{
	$addwhere = "" ;
	$rid = intval($style) ;
	if ($rid == 0){
		$rid = intval(sql_fetch_one_cell("select `id` from grp_style where `name` = '$style'")) ;
	}
	if ($rid > 0){
		//$addwhere = " and `id` in (select `item_id` from sys_item_style where `style_id` = '$rid')" ;
		$addwhere = " and `id` in (select `item_id` from sys_item_style where `style_id` in (SELECT id FROM `grp_style` where id ='$rid'  or father='$rid') )" ;
	}
	return $addwhere ;
}
function _filter_color_item($color)
{
	$addwhere = "" ;
	$rid = intval($color) ;
	if ($rid == 0){
		$rid = intval(sql_fetch_one_cell("select `id` from grp_color where `name` = '$color'")) ;
	}
	if ($rid > 0){
		$addwhere = " and `id` in (select `item_id` from sys_item_color where `color_id` = '$rid')" ;
	}
	return $addwhere ;
}
function _filter_price_item($price_min,$price_max)
{
	$addwhere = "" ;
	$price_min = intval($price_min) ;
	$price_max = intval($price_max) ;
	if ($price_min != 0 || $price_max != 0){
		$addwhere = " and `price_base` + `price_custom` >= $price_min and `price_base` + `price_custom` <= $price_max " ;
	}
	return $addwhere ;
}
function _filter_user_show($user)
{
	$addwhere = "" ;
	$id = intval($user) ;
	if ($id == 1){
		$addwhere = " and `publish_uid` >= 2000 " ;
	}elseif ($id == 2){
		$addwhere = " and `publish_uid` < 2000 " ;
	}
	return $addwhere ;
}
function _filter_type_show($type)
{
	$addwhere = "" ;
	$id = intval($type) ;
	if ($id == 5){
		$addwhere = " and `state` = 0 " ;
	}else{
		$addwhere = " and `state` = '$id' " ;
	}
	return $addwhere ;
}
function _filter_time_show($time)
{
	$addwhere = "" ;
	$id = intval($time) ;
	$tm = 24 * 3600 * $id * 30 ;
	if ($id == 1){		// 最近1个月
		$addwhere = " and (unix_timestamp() - `publish_time`) < $tm " ; 
	}elseif ($id == 2){	// 最近1个月
		$addwhere = " and (unix_timestamp() - `publish_time`) < $tm " ;
	}elseif ($id == 3){	// 最近3个月
		$addwhere = " and (unix_timestamp() - `publish_time`) < $tm " ;
	}
	return $addwhere ;
}
function _filter_room_set($room)
{
	$addwhere = "" ;
	$rid = intval($room) ;
	if ($rid == 0){
		$rid = intval(sql_fetch_one_cell("select `id` from grp_space where `name` = '$room'")) ;
	}
	if ($rid > 0){
		$addwhere = " and `space_id` = '$rid'" ;
	}
	return $addwhere ;
}
function _filter_style_set($style)
{
	$addwhere = "" ;
	$rid = intval($style) ;
	if ($rid == 0){
		$rid = intval(sql_fetch_one_cell("select `id` from grp_style where `name` = '$style'")) ;
	}
	if ($rid > 0){
		$addwhere = " and `sid` in (select `taozhuang_id` from taozhuang_style where `style_id` in (SELECT id FROM `grp_style` where id ='$rid'  or father='$rid'))" ;
	}
	return $addwhere ;
}
function _filter_color_set($color)
{
	$addwhere = "" ;
	$rid = intval($color) ;
	if ($rid == 0){
		$rid = intval(sql_fetch_one_cell("select `id` from grp_color where `name` = '$color'")) ;
	}
	if ($rid > 0){
		$addwhere = " and `sid` in (select `taozhuang_id` from taozhuang_color where `color_id` = '$rid')" ;
	}
	return $addwhere ;
}
function _filter_price_set($price_min,$price_max)
{
	$addwhere = "" ;
	$price_min = intval($price_min) ;
	$price_max = intval($price_max) ;
	if ($price_min != 0 || $price_max != 0){
		$addwhere = " and `price_base` + `price_custom` >= $price_min and `price_base` + `price_custom` <= $price_max " ;
	}
	return $addwhere ;
}
	
// 单品详细信息
function item($json , $session)
{
	$id = get_jsonValue($json,"id") ;
	$uid = util_set_GETUID_for_SESSION($session) ;		
	if ($uid <= 0){
		// 获取缓冲数据
		$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
		$ret = mem_getValues($cache_key) ;
		if ($ret != null){
			return $ret ;
		}
	}
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['item'] = array() ;
	
	util_set_ITEM($ret['data']['item'] , $id , $uid) ;	
	_util_save_user_log($uid , $ret['data']['item']['name'] , "item" , $id ,"") ;
	
	if ($uid <= 0){
		mem_setKey($cache_key, $ret) ;
	}
	return $ret ;
}
// 查询用户信息
function userInfo($json , $session)
{
	$ret = array() ;
	$ret['data'] = array() ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	$ret['data']['user'] = array() ;
	util_set_USER($ret['data']['user'] , $uid) ;
	return $ret ;
}
// 第三方用户登陆
function user_third_login($json)
{
	$ret = array() ;	
	$ret['data'] = array() ;
	$key = "0jtiXere3s7LbCu4Ct6oOwaQUDGo3cTX" ;
	
	$loginType	= get_jsonValue($json,"loginType") ;	// 登陆类型 （1手机.2QQ.3微博.4微信.5邮件）
	$openId		= get_jsonValue($json,"openId") ;		// 用户标识符（用作用户名）
    $nickName 	= get_jsonValue($json,"nickName") ;		// 昵称
    $sex      	= get_jsonValue($json,"sex") ;			// 性别
    $invitecode	= get_jsonValue($json,"invitecode") ;	// 邀请人的邀请码（注册用户才有值，登陆为空）
    $timestamp  = get_jsonValue($json,"timestamp") ;	// 时间戳
    $loginKey 	= strtoupper(get_jsonValue($json,"loginKey")) ;		// openId+nickName+sex+key字符串  md5
    $headImageUrl = get_jsonValue($json,"headImageUrl") ;	// 用户头像
    
    $sign = strtoupper(md5($openId.$loginType.$invitecode.$timestamp.$key)) ;
	
	if (strlen($invitecode) > 0){
		if (sql_check("select `uid` from user_invite where `invite` = '$invitecode'") == false){
			$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_INVITE"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_INVITE"] ;
			return $ret ;
		}
	}
	if ($sign != $loginKey){
		$ret['status'] = $GLOBALS["ERR"]["ERR_PARAM_ERROR"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_PARAM_ERROR"] ;
//		$ret['mem1'] = $openId.$loginType.$invitecode.$timestamp.$key ;
//		$ret['mem2'] = $sign ;
		return $ret ;
	}
	$uinfo = sql_fetch_one("select * from user_info where username = '$openId'") ;
	if (is_null($uinfo) == false && array_key_exists("username" , $uinfo) == true){
		$uid = $uinfo['id'] ;
		
		sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'third_face' , '$headImageUrl')  ON DUPLICATE KEY UPDATE `u_value` = '$headImageUrl'") ;
		
		$tokn = substr(md5($openId . $openId . time()) . $openId , 2 , 38) ; 	
		return _user_getinfo($uid , $tokn) ;
	}
	
	$uinfo = _user_regist_insert($openId , $nickName , $invitecode , $openId , $openId , $openId , $loginType , $sex) ;
	$toke = $uinfo[0] ;
	$uid = $uinfo[1] ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'third_face' , '$headImageUrl')  ON DUPLICATE KEY UPDATE `u_value` = '$headImageUrl'") ;
	
	$ret['data']['session'] = array() ;
	util_set_SESSION($ret['data']['session'] , $toke) ;
	$ret['data']['user'] = array() ;
	util_set_USER($ret['data']['user'] , $uid) ;
	return $ret ;	
}
// 查询该用户是否已注册			
function queryOpenID($json)
{
	$ret = array() ;	
	$ret['data'] = array() ;
	$openId		= get_jsonValue($json,"openId") ;		// 用户标识符（用作用户名）
	
	$uinfo = sql_fetch_one("select * from user_info where username = '$openId'") ;
	if (is_null($uinfo) == false && array_key_exists("username" , $uinfo) == true){
		$ret['data']['isFirstLogin'] = 1 ;
	}else{
		$ret['data']['isFirstLogin'] = 0 ;
	}
	return $ret ;
}
// 用户登录
function user_signin($json)
{
	$username = get_jsonValue($json,"username") ;	// 用户名
	$password = get_jsonValue($json,"password") ;	// 密码
	$ret = array() ;	
	$ret['data'] = array() ;
	
	$pass = substr(md5("gjj" . $password),2,18) ;
	$uinfo = sql_fetch_one("select * from user_info where tel = '$username'") ;
	if (is_null($uinfo) || array_key_exists("password_hash" , $uinfo) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	if ($uinfo['password_hash'] != $pass){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_PASSFIAL"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_PASSFIAL"] ;
		return $ret ;
	}
	$uid = $uinfo['id'] ; 
	$tokn = substr(md5($username . $password . time()) . $pass , 2 , 38) ; 
	
	return _user_getinfo($uid , $tokn) ;
}

function _user_getinfo($uid , $tokn)
{
	$ret = array() ;	
	$ret['data'] = array() ;

	$sess = (rand(343,9999999) * rand(2223,3333322)) % 1000000 ;
	$deadline_day = 60 * 60 * 24 * 30 ;	// 7天后token过期.
	sql_insert("insert into sys_session (`uid` , `session` , `token` , `deadline`) values ('$uid' , '$sess' , '$tokn' , unix_timestamp() + $deadline_day)   ON DUPLICATE KEY UPDATE `session` = '$sess' , `deadline` = unix_timestamp() + $deadline_day") ;	
	sql_query("delete from sys_session where `deadline` > 0 and `deadline` < unix_timestamp()") ;
	
	$ret['data']['session'] = array() ;
	util_set_SESSION($ret['data']['session'] , $tokn) ;
	$ret['data']['user'] = array() ;
	util_set_USER($ret['data']['user'] , $uid) ;
	return $ret ;	
}

// 用户注册
function user_signup($json)
{
	$username = get_jsonValue($json,"username") ;	// 用户名=手机号
	$password = get_jsonValue($json,"password") ;	// 密码
	$sms_token= get_jsonValue($json,"sms_token") ;	// 短信验证码
	
	$invite_code = trim(get_jsonValue($json,"invite_code")) ;		// 邀请码
	
	$ret = array() ;
	$ret['data'] = array() ;
	
	if (strlen($invite_code) > 0){
		if (sql_check("select `uid` from user_invite where `invite` = '$invite_code'") == false){
			$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_INVITE"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_INVITE"] ;
			return $ret ;
		}
	}
	
	sql_query("delete from `sys_token` where `exceed_at` < unix_timestamp()") ;		// 删除过期时间的token
	
	if (sql_check("select * from `sys_token` where `tel` = '$username' and `token` = '$sms_token' and `exceed_at` >= unix_timestamp() and `type` = 1 ") == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_CODE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_CODE"] ;
		return $ret ;
	}
	$uinfo = sql_fetch_one("select * from user_info where username = '$username'  ") ;
	$status=intval($uinfo["status"]);
	//11.14    存在用户，判断是正常用户，还是注销用户
	if (is_array($uinfo) && array_key_exists("password_hash" , $uinfo) == true){
	    //注销用户  直接把status改成1 
	    if($status==2){
	        $result=sql_query("UPDATE   user_info SET `status`=1 WHERE username='$username'  ");
	    //注册用户，提示已注册
	    }elseif($status==1){
	        $ret['status'] = $GLOBALS["ERR"]["ERR_USER_EXIST"] ;
	        $ret['message'] = $GLOBALS["MES"]["ERR_USER_EXIST"] ;
	        return $ret ;
	    }  
	     
	}else{
	    $sex = "男" ;
	    $pass = substr(md5("gjj" . $password),2,18) ;
	    //判断是新注册 还是注销用户
	    $ui = _user_regist_insert($username , $username , $invite_code , $username ,  $password , $pass , 1 , $sex) ;
	    
	    $ret['data']['session'] = array() ;
	    util_set_SESSION($ret['data']['session'] , $ui[0]) ;
	    $ret['data']['user'] = array() ;
	    util_set_USER($ret['data']['user'] , $ui[1]) ;
	    return $ret ;
	}
	
}

// $user_from -- '1手机.2QQ.3微博.4微信.5邮件',
function _user_regist_insert($username , $userice ,$invite_code , $tel , $password , $pass , $user_from , $user_sex)
{
	$uid = sql_insert("insert into user_info (`username` , `nickname` , `tel` , `password_hash` , `created_at` , `status` , `user_type` , `user_from`) values('$username' , '$userice' , '$tel' , '$pass' , unix_timestamp() , 1 , 1 , '$user_from')") ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'img_small_id' , '2464')") ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'img_thumb_id' , '2464')") ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'img_url_id' , '2465')") ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'star_rank' , '0')") ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'user_sex' , '$user_sex')") ;
	
	$sess = (rand(343,9999999) * rand(2223,3333322)) % 1000000 ;
	$tokn = substr(md5($username . $password . time()) . $pass , 2 , 38) ; 
	
	$deadline_day = 60 * 60 * 24 * 30 ;	// 7天后token过期.
	sql_insert("insert into sys_session (`uid` , `session` , `token` , `deadline`) values ('$uid' , '$sess' , '$tokn' , unix_timestamp() + $deadline_day)") ;

	// 注册产生积分
    $add_score = getConfigValue('register_point') ;
    if ($add_score > 0) {
        addUserScore($uid , $add_score , 3 ,"注册赠送" , "regist_insert") ;
    }

	// 2017.4.20后不需要邀请码,自己和别人都不需要,邀请码为空,标示新版本
	if (strlen($invite_code) > 0){		
		$invite = random_mix(6) ;
		while (true){
			if (sql_check("select `uid` from user_invite where `invite` = '$invite'") == false){
				break ;
			}else{
				$invite = random_mix(6) ;
			}
		}
		sql_insert("insert into user_invite (`uid` , `invite` , `invite_f`) values ('$uid' , '$invite' , '$invite_code')") ;
	}

	// 1级上级,赠送
    $add_score = getConfigValue('register_point_L1') ;
	if ($add_score > 0) {
        $inviteuser_1 = sql_fetch_one("select * from user_invite where `invite` = '$invite_code'");
        if (is_array($inviteuser_1)) {
            $inv_uid_1 = $inviteuser_1['uid'];
            $usertype = intval(sql_fetch_one_cell("select `user_type` from user_info where `id` = '$inv_uid_1'")) ;
            if ($usertype > 0) {
                addUserScore($inv_uid_1, $add_score, 3 ,"下级注册赠送", "other1 user regist_insert");
            }

            // 2级上级 赠送
            $add_score = getConfigValue('register_point_L2') ;
            if ($add_score > 0) {
                $invite_code = $inviteuser_1['invite_f'] ;
                $inviteuser_2 = sql_fetch_one("select * from user_invite where `invite` = '$invite_code'");
                if (is_array($inviteuser_2)) {
                    $inv_uid_2 = $inviteuser_2['uid'];
                    $usertype = intval(sql_fetch_one_cell("select `user_type` from user_info where `id` = '$inv_uid_2'"));
                    if ($usertype > 0) {
                        addUserScore($inv_uid_2, $add_score, 3 ,"下下级注册赠送", "other2 user regist_insert");
                    }
                }
            }
        }
    }
	$ret = array() ;
	$ret[] = $tokn ;
	$ret[] = $uid ;
	return $ret ;
}
// 付费设计师付费成功调用.
function _vip_user_add_score($uid)
{
    $add_score = getConfigValue('super_register_point') ;
    if ($add_score > 0){
        addUserScore($uid, $add_score, 3 ,"vip付费赠送", "vip user");
    }
}
// 请求短信验证码
function sms_request($json)
{
	$mobile = get_jsonValue($json , "mobile") ;		// 手机号
	
	$ret = array() ;
	$ret['data'] = array() ;
	
	sql_query("delete from `sys_token` where `exceed_at` < unix_timestamp()") ;		// 删除过期时间的token
	
	if (sql_check("select * from `sys_token` where `tel` = '$mobile' and `type` = 1 ") == true){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_CODENO_OUT"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_CODENO_OUT"] ;
		return $ret ;
	}
	
	$token = rand(83829, 92837346) * time() % 1000000 ;
	$token = "$token" ;
	while (strlen($token) < 6){
		$token = "1".$token ;
	}
	$outtm = $GLOBALS['SMS_CALL_OUTTIME'] ;	// 过期时间 
	$sd = send_mobile_SMS($mobile , "您的短信验证码是: $token") ;
	if ($sd == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_CODE_SEND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_CODE_SEND"] ;
		return $ret ;
	}
	
	sql_query("insert into `sys_token`(`tel` , `token` , `exceed_at` , `type`) values ('$mobile' , '$token' , unix_timestamp() + $outtm + 2 , '1')") ;
	
	$ret['data']['time'] = $outtm ;
	$ret['data']['rnt'] = $sd ;
	return $ret ;
}
// 用户忘记密码
function user_recover($json)
{
	$username = get_jsonValue($json,"username") ;	// 用户名=手机号
	$password = get_jsonValue($json,"password") ;	// 新的密码
	$sms_token= get_jsonValue($json,"sms_token") ;	// 短信验证码
	
	$ret = array() ;
	$ret['data'] = array() ;
	
	if (strlen($password) == 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOEMPTY"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOEMPTY"] ;
		return $ret ;
	}
	
	sql_query("delete from `sys_token` where `exceed_at` < unix_timestamp()") ;		// 删除过期时间的token
	
	if (sql_check("select * from `sys_token` where `tel` = '$username' and `token` = '$sms_token' and `exceed_at` >= unix_timestamp() and `type` = 1 ") == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_CODE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_CODE"] ;
		return $ret ;
	}
	$uinfo = sql_fetch_one("select * from user_info where tel = '$username'") ;
	if (is_array($uinfo) == false || array_key_exists("password_hash" , $uinfo) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$pass = substr(md5("gjj" . $password),2,18) ;
	sql_query("update user_info set password_hash = '$pass' where tel = '$username'") ;
	$uinfo = sql_fetch_one("select * from user_info where tel = '$username'") ;
	if ($uinfo['password_hash'] != $pass){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_PASSFIAL"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_PASSFIAL"] ;
		return $ret ;
	}
	
	$uid = $uinfo['id'] ; 
	$sess = (rand(343,9999999) * rand(2223,3333322)) % 1000000 ;
	$tokn = substr(md5($username . $password . time()) . $pass , 2 , 38) ; 
	
	$deadline_day = 60 * 60 * 24 * 30 ;	// 7天后token过期.
	sql_insert("replace into sys_session (`uid` , `session` , `token` , `deadline`) values ('$uid' , '$sess' , '$tokn' , unix_timestamp() + $deadline_day)") ;
	
	$ret['data']['session'] = array() ;
	util_set_SESSION($ret['data']['session'] , $tokn) ;
	$ret['data']['user'] = array() ;
	util_set_USER($ret['data']['user'] , $uid) ;
	return $ret ;	
}
// 用户信息修改
function user_info($json , $session)
{
	$nickname 	= trim(get_jsonValue($json,"nickname")) ;
	$sex		= trim(get_jsonValue($json,"sex")) ;
	$birthday	= trim(get_jsonValue($json,"birthday")) ;
	$mobile		= trim(get_jsonValue($json,"mobile")) ;
	
	$ret = array() ;
	$ret['data'] = array() ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	if (strlen($nickname) > 0){
		sql_query("update user_info set `nickname` = '$nickname' where `id` = '$uid'") ;
	}
	if (strlen($sex) > 0){
		sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'user_sex' , '$sex')  ON DUPLICATE KEY UPDATE `u_value` = '$sex'") ;
	}
	if (strlen($birthday) > 0){
		sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'birthday' , '$birthday')  ON DUPLICATE KEY UPDATE `u_value` = '$birthday'") ;
	}
	if (strlen($mobile) > 0){
		sql_query("update user_info set `tel` = '$mobile' where `id` = '$uid'") ;
	}
	
	$ret['data']['user'] = array() ;
	util_set_USER($ret['data']['user'] , $uid) ;
	return $ret ;	
}
// 用户修改密码
function user_changepassword($json , $session)
{
	$old_password = trim(get_jsonValue($json,"old_password")) ;
	$new_password = trim(get_jsonValue($json,"new_password")) ;
		
	$ret = array() ;
	$ret['data'] = array() ;
	
	if (strlen($old_password) == 0 || strlen($new_password) == 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOEMPTY"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOEMPTY"] ;
		return $ret ;
	}
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}		
	$uinfo = sql_fetch_one("select * from user_info where `id` = '$uid'") ;
	if (is_array($uinfo) == false || array_key_exists("password_hash" , $uinfo) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	$old_p = substr(md5("gjj" . $old_password),2,18) ;
	if ($old_p != $uinfo['password_hash']){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_PASSFIAL"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_PASSFIAL"] ;
		return $ret ;
	}
	
	$pass = substr(md5("gjj" . $new_password),2,18) ;
	sql_query("update user_info set password_hash = '$pass' where `id` = '$uid'") ;
	$uinfo = sql_fetch_one("select * from user_info where `id` = '$uid'") ;
	if ($uinfo['password_hash'] != $pass){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_PASSCHANGE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_PASSCHANGE"] ;
		return $ret ;
	}
	return $ret ;
}
// 用户消息
function user_message($json, $session)
{
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$ret = array() ;
	$ret['data'] = array() ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}		
	
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(u.msgid) from user_message u inner join sys_message s on u.`msgid` = s.`id` and s.`status` = 3 where u.`uid` = $uid") ;
		
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
			
	$ret['data']['new'] = sql_fetch_one_cell("select count(u.msgid) from user_message u inner join sys_message s on u.`msgid` = s.`id` and s.`status` = 3 where u.`uid` = $uid and u.`read` = 0") ;
	$ret['data']['message'] = array() ;
	
	$mess = sql_fetch_rows("select u.read,s.* from user_message u inner join sys_message s on u.`msgid` = s.`id` and s.`status` = 3 where u.`uid` = $uid order by s.create_time desc limit $start , $limit") ;
	foreach ($mess as $m)
	{
		$v = array() ;
		util_set_MESSAGE($v , $m) ;
		$ret['data']['message'][] = $v ;
	}
	return $ret ;
}
// 用户消息处理
function user_message_operation($json, $session)
{
	$id 		= intval(get_jsonValue($json,"id")) ;
	$operation	= intval(get_jsonValue($json,"operation")) ;		// 1 -- 改为已读  , 2 -- 删除
	$ret = array() ;
	$ret['data'] = array() ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$where = "" ;
	if ($id == 0){
		$where = " where `uid` = '$uid'" ;
	}else{
		$where = " where `uid` = '$uid' and `msgid` = '$id'" ;
	}
	
	if ($operation == 1){
		sql_query("update user_message set `read` = 1 $where") ;
	}elseif ($operation == 2){
		sql_query("delete from user_message $where") ;
	}else{
		$ret['status'] = $GLOBALS["ERR"]["ERR_PARAM_ERROR"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_PARAM_ERROR"] ;
		return $ret ;
	}
	return $ret ;	
}
// 用户反馈
function feedback($json , $session)
{
	$title 	= trim(get_jsonValue($json,"title")) ;	//标题
	$content= trim(get_jsonValue($json,"content")) ;	//内容
	$ret = array() ;
	$ret['data'] = array() ;
	
	if ($title == "" && $content == "")
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_PARAM_ISNULL"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_PARAM_ISNULL"] ;
		return $ret ;
	}
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$newid = sql_insert("INSERT INTO `sys_user_feedback` (`uid`, `time`, `title`, `content`) VALUES ('$uid', unix_timestamp(), '$title', '$content')") ;
	if ($newid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_DATABASE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_DATABASE"] ;
		return $ret ;
	}
	return $ret ;
}
// 用户头像上传
function user_avatar($json , $session)
{
	$avatar = get_jsonValue($json,"avatar") ;	// 头像图片数据经过 base64 编码
	$ret = array() ;
	$ret['data'] = array() ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$data = base64_decode($avatar) ;
	if ($data == FALSE){
		$ret['status'] = $GLOBALS["ERR"]["ERR_BASE64"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_BASE64"] ;
		return $ret ;
	}
	$newid = image_data_save("" , $data , $ret) ;
	if ($newid == 0){
		return $ret ;
	}
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'img_small_id' , '$newid') ON DUPLICATE KEY UPDATE `u_value` = '$newid'") ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'img_thumb_id' , '$newid') ON DUPLICATE KEY UPDATE `u_value` = '$newid'") ;
	sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'img_url_id' , '$newid') ON DUPLICATE KEY UPDATE `u_value` = '$newid'") ;
		
	$ret['data']['user'] = array() ;
	util_set_USER($ret['data']['user'] , $uid) ;
	return $ret ;
}

// 秀家列表
function showlist($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
		
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;	
	$user 	= get_jsonValue($filter , "user") ;		if (strlen($user) > 0)	$addwhere .= _filter_user_show(intval($user)) ;		// 用户
	$type	= get_jsonValue($filter , "type") ;		if (strlen($type) > 0)	$addwhere .= _filter_type_show(intval($type)) ;		// 类型
	$time 	= get_jsonValue($filter , "time") ;		if (strlen($time) > 0)	$addwhere .= _filter_time_show(intval($time)) ;		// 时间
	
	$name 	= trim(get_jsonValue($filter , "name")) ;
	if (strlen($name) > 0){
		$addwhere .= " and `name` like '%$name%' " ;
	}
	
	$b_test = intval(get_jsonValue($json,"is_test")) ;
	$where_apprnt = " `app_return` = 1 " ;
	if ($b_test == 1){
		$where_apprnt = " 1 = 1 " ;
	}
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['item'] = array() ;
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(id) from `show` where $where_apprnt $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	

	$bitem = sql_fetch_rows("select * from `show` where $where_apprnt $addwhere order by `top_time` desc ,`publish_time` desc , `id` desc limit $start , $limit") ;	
	foreach ($bitem as &$b){
		$rn = array() ;
		util_set_SHOW_SIMPLE($rn , $b) ;
		if ($b_test == 1){
			$rn['img_small_id'] = $b['img_small_id'] ;
			$rn['img_thumb_id'] = $b['img_thumb_id'] ;
			$rn['img_url_id'] = $b['img_url_id'] ;
		}
		$ret['data']['item'][] = $rn ;
		$b['zs_item'] = $rn ;
	}
	if ($b_test == 1){
		$ret['data']['lists'] = $bitem ;
	}
	
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}

// 秀家列表
function showlist_v2($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
		
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$idlist = get_jsonValue($json,"idArray") ;	
	
	if (is_array($idlist) && count($idlist) > 0){
		$addwhere = " and `state` in (" ;
		$dh = "" ;
		foreach($idlist as $id){
			if ($id == 5){
				$addwhere .= $dh . " 0 " ;	$dh = "," ;
			}else{
				$addwhere .= $dh . " $id " ;	$dh = "," ;
			}
		}
		$addwhere .= ")" ;
	}
	
	$b_test = intval(get_jsonValue($json,"is_test")) ;
	$where_apprnt = " `app_return` = 1 " ;
	if ($b_test == 1){
		$where_apprnt = " 1 = 1 " ;
	}
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['item'] = array() ;
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(id) from `show` where $where_apprnt $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	

	$bitem = sql_fetch_rows("select * from `show` where $where_apprnt $addwhere order by `top_time` desc ,`publish_time` desc , `id` desc limit $start , $limit") ;	
	foreach ($bitem as &$b){
		$rn = array() ;
		util_set_SHOW_SIMPLE($rn , $b) ;
		if ($b_test == 1){
			$rn['img_small_id'] = $b['img_small_id'] ;
			$rn['img_thumb_id'] = $b['img_thumb_id'] ;
			$rn['img_url_id'] = $b['img_url_id'] ;
		}
		$ret['data']['item'][] = $rn ;
		$b['zs_item'] = $rn ;
	}
	if ($b_test == 1){
		$ret['data']['lists'] = $bitem ;
	}
	
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
// 秀家信息
function show($json , $session)
{
	// 获取缓冲数据
	$is_test = intval(get_jsonValue($json,"is_test")) ;	
	$uid = util_set_GETUID_for_SESSION($session) ;
	
	if ($uid <= 0){
		// 获取缓冲数据
		$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
		$ret = mem_getValues($cache_key) ;
		if ($ret != null){
			return $ret ;
		}
	}
			
	$id = get_jsonValue($json,"id") ;
	
	$ret = array() ;
	$ret['data'] = array() ;
	
	$item = sql_fetch_one("select * from `show` where `id` = '$id'") ;
	if (is_null($item) || array_key_exists('id' , $item) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NO_DATA"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NO_DATA"] ;
		return $ret ;
	}
	
	$ret['data']['show'] = array() ;
	util_set_SHOW($ret['data']['show'] , $item , $uid) ;
	_util_save_user_log($uid , $ret['data']['show']['name'] , "show" , $id ,"") ;
	
	if ($uid <= 0){
		mem_setKey($cache_key, $ret) ;
	}
	return $ret ;
}
// 用户收藏
function add_favorite($json , $session)
{
	$type = get_jsonValue($json,"type") ;
	$id = get_jsonValue($json,"id") ;
	$ret = array() ;
	$ret['data'] = array() ;
	if ($type != 0 && $type != 1 && $type != 2 && $type != 3){
		$ret['status'] = $GLOBALS["ERR"]["ERR_PARAM_ERROR"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_PARAM_ERROR"] ;
		return $ret ;
	}
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}

	sql_insert("INSERT INTO `user_favorite` (`uid`, `type`, `type_id`, `addtime`) VALUES ('$uid', '$type', '$id', unix_timestamp())  ON DUPLICATE KEY UPDATE `addtime` = unix_timestamp()") ;
	
	return $ret ;
}
// 获取用户套装收藏夹
function favorite_set($json , $session)
{
	$ret = array() ;
	$ret['data'] = array() ;
	util_get_PAGINATION($json , $offset , $limit) ;
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(type_id) from user_favorite where `uid` = '$uid' and `type` = 0") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select t.* from taozhuang t inner join user_favorite u on t.sid = u.type_id where u.`uid` = '$uid' and u.`type` = 0 order by t.`top_time` desc , u.`addtime` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_SET_SIMPLE($rn , $b) ;
		$ret['data']['set'][] = $rn ;
	}
	return $ret ;
}
// 获取用户单品收藏夹
function favorite_item($json , $session)
{
	$ret = array() ;
	$ret['data'] = array() ;
	util_get_PAGINATION($json , $offset , $limit) ;
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(type_id) from user_favorite where `uid` = '$uid' and `type` = 1") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select s.* from `sys_item` s inner join user_favorite u on s.id = u.type_id  where u.`uid` = '$uid' and u.`type` = 1 order by s.`class_id` asc ,s.`top_time` desc ,u.`addtime` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_ITEM_SIMPLE($rn , $b) ;	
		$ret['data']['item'][] = $rn ;
	}
	return $ret ;
}
// 获取用户秀家收藏夹
function favorite_show($json , $session)
{
	$ret = array() ;
	$ret['data'] = array() ;
	util_get_PAGINATION($json , $offset , $limit) ;
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(type_id) from user_favorite where `uid` = '$uid' and `type` = 2") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select s.* from `show` s inner join user_favorite u on s.id = u.type_id  where u.`uid` = '$uid' and u.`type` = 2 order by s.`top_time` desc ,u.`addtime` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_SHOW_SIMPLE($rn , $b) ;	
		$ret['data']['show'][] = $rn ;
	}
	return $ret ;
}
// 删除收藏夹
function del_favorite($json , $session)
{
	$type = get_jsonValue($json,"type") ;
	$id = get_jsonValue($json,"id") ;
	$ret = array() ;
	$ret['data'] = array() ;
	if ($type != 0 && $type != 1 && $type != 2 && $type != 3){
		$ret['status'] = $GLOBALS["ERR"]["ERR_PARAM_ERROR"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_PARAM_ERROR"] ;
		return $ret ;
	}
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}

	sql_query("delete from `user_favorite` where `uid` = '$uid' and `type` = '$type' and `type_id` = '$id'") ;
	
	return $ret ;
}

// 获取配置信息
function config()
{
	// 获取缓冲数据
	$cache_key = "config" ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['config'] = array() ;	
	util_set_CONFIG($ret['data']['config']) ;
	
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
// 获取配置信息
function config_v2($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid > 0){
		$token = trim(get_jsonValue($session,"token")) ; 		
		$deadline_day = 60 * 60 * 24 * 30 ;	// 7天后token过期.

		sql_query("update sys_session set `deadline` = unix_timestamp() + $deadline_day where `uid` = '$uid' and `token` = '$token'") ;
	}
	
	
	// 获取缓冲数据
	$city = trim(get_jsonValue($json,"city")) ;
	$cache_key = "config_v2" . $city ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}

	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['config'] = array() ;	
	util_set_CONFIG_V2($ret['data']['config'] , $city) ;
	
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}

// 收获地址列表
function addresslist($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$ret['address'] = array() ;
	$adds = sql_fetch_rows("select * from user_address where `uid` = '$uid'") ;
	foreach ($adds as $a){
		$n = array() ;
		util_set_ADDRESS($n , $a) ;
		$ret['address'][] = $n ;
	}
	return $ret ;
}
// 用户信息修改
function save_address($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	$address = get_jsonValue($json,"address") ;
	$id 			= intval(get_jsonValue($address , "id")) ;
	$area			= get_jsonValue($address , "area") ;
	$detail_address	= get_jsonValue($address , "detail_address") ;
	$postcode		= get_jsonValue($address , "postcode") ;
	$name			= get_jsonValue($address , "name") ;
	$mobile			= get_jsonValue($address , "mobile") ;
	$tel			= get_jsonValue($address , "tel") ;
	$type 			= get_jsonValue($address , "type") ;

	
	if ($id == 0){
		$newid = sql_insert("insert into user_address(`uid` , `area` , `address` , `postcode` , `name` , `mobile` , `tel` , `type`) values ('$uid' , '$area' , '$detail_address' , '$postcode' , '$name' , '$mobile' , '$tel' , '$type')") ;
		$id = $newid ;
	}else{
		if (sql_check("select * from user_address where `id` = '$id' and `uid` = '$uid'") == false)
		{
			$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
			return $ret ;
		}
		sql_query("update user_address set `area` = '$area' , `address` = '$detail_address' , `postcode` = '$postcode'  , `name` = '$name'  , `mobile` = '$mobile'  , `tel` = '$tel'  , `type` = '$type' where `id` = '$id'") ;
	}
	
	if ($type == 1){
		sql_query("update user_address set `type` = 0 where `uid` = '$uid' and `id` != '$id'") ;
	}
	if (intval(sql_fetch_one_cell("select count(*) from user_address where `uid` = '$uid' and `type` = 1")) != 1){
		sql_query("update user_address set `type` = 0 where `uid` = '$uid' and `id` != '$id'") ;
		sql_query("update user_address set `type` = 1 where `uid` = '$uid' and `id` = '$id'") ;
	}
	
	$item = sql_fetch_one("select * from user_address where `id` = '$id'") ;
	$ret['address'] = array() ;
	util_set_ADDRESS($ret['address'] , $item) ;
	return $ret ;	
}
// 删除收货地址
function del_address($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	$id 	= intval(get_jsonValue($json , "id")) ;
	if (sql_check("select * from user_address where `id` = '$id' and `uid` = '$uid'") == false)
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
		return $ret ;
	}
	sql_query("delete from user_address where `id` = '$id'") ;
	
	$ret['address'] = array() ;
	$adds = sql_fetch_rows("select * from user_address where `uid` = '$uid'") ;
	foreach ($adds as $a){
		$n = array() ;
		util_set_ADDRESS($n , $a) ;
		$ret['address'][] = $n ;
	}
	return $ret ;
}
// 省市列表
function arealist($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
	
	$ret = array() ;
	$ret['data'] = array() ;	
	$ret['data']['area'] = array() ;
	
	$bitem = sql_fetch_rows("select * from grp_city where `id` like '%00'") ;
	foreach ($bitem as $b){		
		util_set_AREA($ret['data']['area'] , $b) ;	
	}
	
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
function citylist($json)
{
	$ret = array() ;
	$ret['data'] = array() ;	
	$ret['data']['city'] = array() ;
	
	$citys = sql_fetch_array("select * from grp_city" , 'id') ;
	$bitem = sql_fetch_rows("select * from bulid_city") ;
	foreach ($bitem as $b){		
		util_set_CITYLIST($ret['data']['city'] , $citys , $b) ;	
	}
	return $ret ;
}

// 获取楼盘列表
function buliding_list($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
		
	util_get_PAGINATION($json , $offset , $limit) ;
	$start = ($offset - 1) * $limit ;
	
	
	$ret = array() ;
	$ret['data'] = array() ;	
	$ret['data']['building'] = array() ;
	
	$cityid = intval(get_jsonValue($json,"city")) ;
	$cityid2 = 0 ;
	
	if (intval($cityid / 10000) * 10000 == $cityid){
		$cityid2 = intval($cityid / 10000) * 10000 + 9999 ;
	}else if (intval($cityid / 100) * 100 == $cityid){
		$cityid2 = intval($cityid / 100) * 100 + 99 ;
	}
	
	if ($cityid2 == 0){
		$where = " where city_id = '$cityid' " ;
	}else{
		$where = " where city_id >= '$cityid' and city_id < '$cityid2' " ;
	}
		
	$list = sql_fetch_rows("select * from bulid_city $where order by `sort` desc , `id` desc  limit $start , $limit") ;
	foreach ($list as $b){
		$rn = array() ;
		util_set_BULIDING($rn , $b) ;	
		$ret['data']['building'][] = $rn ;
	}
	
	$allcount = sql_fetch_one_cell("select count(id) from bulid_city $where") ;	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
// 获取户型列表
function house_list($json)
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
		
	$ret = array() ;
	$ret['data'] = array() ;	
	$ret['data']['house'] = array() ;
	
	$bid = get_jsonValue($json,"id") ;		// 楼盘编号
	
	util_get_PAGINATION($json , $offset , $limit) ;
	$start = ($offset - 1) * $limit ;	
	
	$list = sql_fetch_rows("select * from build_type where bid = '$bid' limit $start , $limit") ;
	foreach ($list as $b){
		$rn = array() ;
		util_set_HOUSETYPE($rn , $b) ;	
		$ret['data']['house'][] = $rn ;
	}
	
	$allcount = sql_fetch_one_cell("select count(id) from build_type where `bid` = '$bid'") ;	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
		
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
// 获取户型相关套装列表
function house_setlist($json)
{	
		// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}

	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	$room 	= get_jsonValue($filter , "room") ;			$addwhere .= _filter_room_set(intval($room)) ;		// 空间
	$style	= get_jsonValue($filter , "style") ;		$addwhere .= _filter_style_set(intval($style)) ;	// 风格
	$color 	= get_jsonValue($filter , "color") ;		$addwhere .= _filter_color_set(intval($color)) ;	// 色系
	$price	= get_jsonValue($filter , "price") ;		// 价格区间
	$price_min = get_jsonValue($price , "min") ;
	$price_max = get_jsonValue($price , "max") ;		$addwhere .= _filter_price_item(intval($price_min) , intval($price_max)) ;	// 价格区间
	$order_by = get_jsonValue($filter , "order_by") ;// 排序方式
	$brand  = intval(get_jsonValue($filter, "brand")) ;	// 品牌+类别
	if ($brand > 0){
		if ($brand > 1000){
			$classid = intval($brand / 1000) * 1000 ;
			$brandid = $brand % 1000 ;
			$addwhere .= " and `sid` in (select p.tzid from taozhuang_pos p inner join sys_item s on p.item_id = s.id where s.class_id = $classid and s.brand_id = $brandid group by p.tzid)" ;
		}else{
			$brandid = $brand ;
			$addwhere .= " and `sid` in (select p.tzid from taozhuang_pos p left join sys_item s on p.item_id = s.id where s.brand_id = $brandid group by p.tzid)" ;
		}
	}
	
	$hid = get_jsonValue($json,"id") ;		// 户型编号
	
	$tzflg = get_jsonValue($json,"tzflag") ;		// 可选参数，1 不加套装一览，没有加套装一览
	
	$hliststr = sql_fetch_one_cell("select tz_img_list from build_type where `id` = '$hid'") ;
	if (strlen($hliststr) > 0){
		$addwhere .= " and `sid` in ($hliststr)" ;
	}
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(sid) from taozhuang where `app_return` = 1 $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
    if (strlen($hliststr) > 0){
		$bitem = sql_fetch_rows("select * from taozhuang where `app_return` = 1 $addwhere order by field(sid,$hliststr) limit $start , $limit") ;
	} else {
		//如果tzflag=1,不需要展示全部套装。
		if ($tzflg == 1 ){
			$addwhere .= " and `sid` in ('')" ;
		}
		
		$bitem = sql_fetch_rows("select * from taozhuang where `app_return` = 1 $addwhere order by `top_time` desc,`sid` desc  limit $start , $limit") ;	
	}	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_SET_SIMPLE($rn , $b) ;
		$ret['data']['set'][] = $rn ;
	}
	
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;
}
//新增接口 获取vip 以及超级vip的金额
function designer_getfeeprice(){
    $feeprice=$GLOBALS['vip_fee'];
    return $feeprice;
}
// 申请设计师
function designer_reg($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$realname = get_jsonValue($json,"realname") ;		// 真实姓名
	$company = get_jsonValue($json,"company") ; 		// 公司名称
    $card_id = get_jsonValue($json,"card_id") ; 		// 身份证号码
    $image_idcard_a = get_jsonValue($json,"image_idcard_a") ; 	// 身份证正面图片经过 base64 编码
    $image_idcard_b = get_jsonValue($json,"image_idcard_b") ; 	// 身份证反面图片经过 base64 编码
    $image_bcard = get_jsonValue($json,"image_bcard") ; 		// 名片图片经过 base64 编码
    $address = get_jsonValue($json,"address") ; 			// 地区列表
	$statues = intval(get_jsonValue($json,"statues")) ; 	// 是否具有售后能力（0->没有， 1->有）
    if ($statues != 0 && $statues != 1)		$statues = 0 ;
	$invite_code = trim(get_jsonValue($json,"invite_code")) ;		// 别人的邀请码
	if (strlen($invite_code) > 0){
		if (sql_check("select `uid` from user_invite where `invite` = '$invite_code'") == false){
			$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_INVITE"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_INVITE"] ;
			return $ret ;
		}
	}
	
	
    $uinfo = sql_fetch_one("select * from sys_user_designer where `uid` = '$uid'") ;
    if (is_array($uinfo) == false || $uinfo['uid'] != $uid)
    {
    	// 没有数据,新建立设计师记录
    	sql_query("INSERT INTO `sys_user_designer` (`uid`, `name`, `company`, `id_card`, `image_idcard_a`, `image_idcard_b`, `image_bcard`, `audit_state`, `audit_uid`, `audit_time`,`audit_memo`) VALUES ('$uid', '$realname', '$company', '$card_id', '0', '0', '0', '0', '0', '0','')") ;
    	$uinfo = sql_fetch_one("select * from sys_user_designer where `uid` = '$uid'") ;
    }
    if ($uinfo['audit_state'] == 1)
    {
    	$ret['status'] = $GLOBALS["ERR"]["ERR_AUDIT_OK"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_AUDIT_OK"] ;
		return $ret ;
    }
    
    // 身份证正面图片经过 base64 编码
	$data_idcard_a = base64_decode($image_idcard_a) ;
	if ($data_idcard_a == FALSE){
		$ret['status'] = $GLOBALS["ERR"]["ERR_BASE64"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_BASE64"] ;
		return $ret ;
	}
	if (intval($uinfo['image_idcard_a']) <= 0){
		$newid_idcard_a = image_data_save("190",$data_idcard_a , $ret) ;
	}else{
		$newid_idcard_a = image_data_change(intval($uinfo['image_idcard_a']),$data_idcard_a , $ret) ;
	}
	if ($newid_idcard_a == 0)	return $ret ;		
	
	
	// 身份证反面图片经过 base64 编码
	$data_idcard_b = base64_decode($image_idcard_b) ;
	if ($data_idcard_b == FALSE){
		$ret['status'] = $GLOBALS["ERR"]["ERR_BASE64"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_BASE64"] ;
		return $ret ;
	}
	if (intval($uinfo['image_idcard_b']) <= 0){
		$newid_idcard_b = image_data_save("190",$data_idcard_b , $ret) ;
	}else{
		$newid_idcard_b = image_data_change(intval($uinfo['image_idcard_b']),$data_idcard_b , $ret) ;
	}
	if ($newid_idcard_b == 0)	return $ret ;
		
		
	// 名片图片经过 base64 编码
	if (strlen($image_bcard) == 0){
		$data_bcard = "" ;
	}else{
		$data_bcard = base64_decode($image_bcard) ;
		if ($data_bcard == FALSE){
			$data_bcard = "" ;
		}
	}
	
	$set_bcard = "" ;
	if (strlen($data_bcard) > 10){
		if (intval($uinfo['image_bcard']) <= 0){
			$newid_bcard = image_data_save("191" ,$data_bcard , $ret) ;
		}else{
			$newid_bcard = image_data_change(intval($uinfo['image_bcard']),$data_bcard , $ret) ;
		}
		if ($newid_bcard == 0)		return $ret ;
		else{
			$set_bcard = " , `image_bcard`='$newid_bcard' " ;
		}
	}
	
	sql_query("UPDATE `user_info` SET user_type = 2 WHERE `id`='$uid'") ;
	sql_query("UPDATE `sys_user_designer` SET `apply_time` = unix_timestamp() , `address`='$address',`name`='$realname', `company`='$company', `statues` = '$statues'  , `id_card`='$card_id', `image_idcard_a`='$newid_idcard_a', `image_idcard_b`='$newid_idcard_b'  $set_bcard  , `audit_state`='0' WHERE (`uid`='$uid')") ;


	if (strlen($invite_code) > 0){
		sql_insert("insert into user_info_ex (`user_id` , `u_key` , `u_value`) values ('$uid' , 'invite_f' , '$invite_code')  ON DUPLICATE KEY UPDATE `u_value` = '$invite_code'") ;
	}
	

	$ret['data'] = array() ;
	$ret['data']['user'] = array() ;
	util_set_USER($ret['data']['user'] , $uid) ;
	return $ret ;
}
// 邀请人列表
function invite_list($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$uinvite = sql_fetch_one_cell("select `invite` from user_invite where `uid` = '$uid'") ;
	if (strlen($uinvite) == 0)
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_INVITE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_INVITE"] ;
		return $ret ;
	}
	
	$offset = 0 ;	$limit = 0 ;
	util_get_PAGINATION($json , $offset , $limit) ;
	$start = ($offset - 1) * $limit ;	
	
	$allcount = sql_fetch_one_cell("select count(u.uid) from user_invite u inner join user_info i on u.uid = i.id where u.invite_f = '$uinvite'") ;
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$ret['data']['invites'] = array() ;
	$arr = sql_fetch_rows("select i.id,i.`username`,i.`nickname`,i.`tel`,from_unixtime(created_at) as time_str from user_invite u inner join user_info i on u.uid = i.id where u.invite_f = '$uinvite' order by `created_at` desc limit $start , $limit") ;
	foreach ($arr as $b){
		$rn = array() ;
		util_set_INVITE($rn , $b) ;
		$ret['data']['invites'][] = $rn ;
	}
	$ret['data']['total'] = array() ;
	util_set_INVITE_TOTAL($ret['data']['total'] , $uid) ;
	return $ret ;
}
// 购物车商品总数
function shopCart_number($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$ret['data'] = array() ;
	$ret['data']['number'] = intval(sql_fetch_one_cell("select count(`id`) from `user_shopping` where `user_id` = '$uid'")) ;
	return $ret ;
}

// 套装清单
function set_item_list($json)
{
	$idlist 	=	get_jsonValue($json,"item_id_list") ; 		// `SET_ITEMINFO`对象的id数组 | 换装后的 单品id 数组
	
	$ret['data'] = array() ;
	$ret['data']['item_list'] = array() ;
	
	foreach($idlist as $id){
		$a = sql_fetch_one("select * from taozhuang_pos where `id` = '$id'") ;
		if (is_array($a)){
			$v = array() ;		
			util_set_SET_POS_ITEM($v , $a , 0) ;
			$ret['data']['item_list'][] = $v ;
		}
	}
	
	return $ret ;
}
// 申请退款
function order_refund_apply($json , $session)
{
 	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$id =  get_jsonValue($json,"id") ; // | 字符串 | 订单编号
	
	$one_trade = sql_fetch_one("select * from pay_trade where `trade_no` = '$id'") ;
	// 订单不存在
	if (is_null($one_trade) == true || array_key_exists('user_id', $one_trade) == false || $one_trade['user_id'] != $uid){
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_INVITE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_INVITE"] ;
		return $ret ;
	}
    if (intval($one_trade['trade_class']) == 2){
        $ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_INVITE"] ;
        $ret['message'] = "众筹订单无法申请退款" ;
        return $ret ;
    }
	// 订单未付款
	if (intval($one_trade['price_state']) != 1 && intval($one_trade['trade_state']) != 1){
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_NOTTRADE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_NOTTRADE"] ;
		return $ret ;
	}
	$log_trade = sql_fetch_one("select * , unix_timestamp() as NowTime from log_pay where `trade_no` = '$id' order by `starttime` asc") ;
	if (intval($log_trade['state']) != 2){
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_NOTTRADE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_NOTTRADE"] ;
		return $ret ;
	}

	$nowtm = intval($log_trade['NowTime']) ;
	$fktm = intval($log_trade['endtime']) ;
	$subtm = $nowtm - $fktm ;	
	$rmb_tk = doubleval($log_trade['rmb']) ;			// 支付的金额
	$rmb_tk = doubleval($one_trade['price_modify']) ;	// 总金额
	//    status_time  状态:
	$status_time = -1 ;
	$refund_bfb = 0 ;
    //* 0 -> 24小时内退款  --- 全额退款
    if ($subtm <= 24 * 60 * 60){
    	$status_time = 0 ;
    	$refund_bfb = 100 ;    	
    	$ret['data']['status_time'] = $status_time ;
		$ret['data']['order_desc'] = "24小时内全额退款" ;
    }
    //* 1 -> 24小时后7天内（含7天）收取订单总额的30%。
    elseif ($subtm <= 24 * 60 * 60 * 7){
    	$status_time = 1 ;
    	$refund_bfb = 70 ;
    	$ret['data']['status_time'] = $status_time ;
		$ret['data']['order_desc'] = "24小时外7天以内退款,需扣款30%" ;
    }
    //* 3 -> 7天以后  --- 不能退款
	else{
    	$status_time = 3 ;    	
		$ret['data']['status_time'] = $status_time ;
		$ret['data']['order_desc'] = "付款超过7天,无法申请退款,请联系客服" ;
		return $ret ;
    }
    return $ret ;
}
// 确认退款/取消退款
function order_refund_confirm($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$id =  get_jsonValue($json,"id") ; // | 字符串 | 订单编号
    $status_refund  =  intval(get_jsonValue($json,"status_refund")) ;		// | 数值 | 是否确认退款 （0 --> 取消退款， 1-->确认退款）
    
    $one_trade = sql_fetch_one("select * from pay_trade where `trade_no` = '$id'") ;
	
	// 订单不存在
	if (is_null($one_trade) == true || array_key_exists('user_id', $one_trade) == false || $one_trade['user_id'] != $uid){
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_INVITE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_INVITE"] ;
		return $ret ;
	}
	
	if ($status_refund == 0)
	{
		// 取消退款
		if (intval($one_trade['price_state']) != 2 && intval($one_trade['trade_state']) != 4){
			$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_NOREFUND"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_NOREFUND"] ;
			return $ret ;			
		}
	}
	// 开始退款
	// 订单未付款
	if (intval($one_trade['price_state']) != 1 && intval($one_trade['trade_state']) != 1){
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_NOTTRADE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_NOTTRADE"] ;
		return $ret ;
	}
	$log_trade = sql_fetch_rows("select * , unix_timestamp() as NowTime from log_pay where `trade_no` = '$id' and `state` = 2 order by `starttime` asc") ;
	if (is_array($log_trade) == false || count($log_trade) == 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_NOTTRADE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_NOTTRADE"] ;
		return $ret ;
	}
	
	
	$nowtm = intval($log_trade[0]['NowTime']) ;
	$fktm = intval($log_trade[0]['endtime']) ;
	$subtm = $nowtm - $fktm ;	
	
	//    status_time  状态:
	$status_time = -1 ;
	$refund_bfb = 0 ;
    //* 0 -> 24小时内退款  --- 全额退款
    if ($subtm <= 24 * 60 * 60){
    	$status_time = 0 ;
    	$refund_bfb = 100 ;    	
    	$ret['data']['status_time'] = $status_time ;
		$ret['data']['order_desc'] = "24小时内全额退款" ;
    }
    //* 1 -> 24小时外7天以内退款,需扣款30%
    elseif ($subtm <= 24 * 60 * 60 * 7){
    	$status_time = 1 ;
    	$refund_bfb = 70 ;    	
    	$ret['data']['status_time'] = $status_time ;
		$ret['data']['order_desc'] = "24小时外7天以内退款,需扣款30%" ;
    }
    //* 3 -> 7天以后  --- 不能退款
	else{
    	$status_time = 3 ;
    	
    	$ret['data']['order'] = array() ;
		util_set_ORDER($ret['data']['order'] , $one_trade) ;
		$ret['data']['status_time'] = $status_time ;
		$ret['data']['order_desc'] = "付款超过7天,无法申请退款,请联系客服" ;
		return $ret ;
    }
    
    $zf_money = 0 ;		// 支付的金额
    foreach($log_trade as $lt){
    	$zf_money += doubleval($lt['rmb']) ;
    }
	$rmb_all = doubleval($one_trade['price_modify']) ;	// 总金额
	
	// 退款额
	//$rmb_tk = $zf_money - $rmb_all * (100 - pay_trade) / 100 ;	
	$rmb_tk = $zf_money - $rmb_all * (100 - $refund_bfb) / 100 ;	

	if ($rmb_tk <= 0){
		$ret['status'] = -100 ;
		$ret['message'] = "退款额计算错误" ;
		return $ret ;
	}
	sql_query("update `pay_trade` set `price_state` = 2 , `trade_state` = 4 , `refund_time` = unix_timestamp() , `refund_bfb` = '$refund_bfb' , `refund_price` = '$rmb_tk' where `trade_no` = '$id'") ;
    
    $one_trade = sql_fetch_one("select * from pay_trade where `trade_no` = '$id'") ;
    $ret['data']['order'] = array() ;
	util_set_ORDER($ret['data']['order'] , $one_trade) ;
    return $ret ;
}

// 套装搜索
function set_search($json)
{
	util_get_PAGINATION($json , $offset , $limit) ;
	
    $search_text = get_jsonValue($json,"search_text") ; // 字符串 | 关键词
    
    $ret['status'] = -1 ;
	$ret['message'] = "未完成" ;
	return $ret ;
}
// 单品搜索
function item_search($json)
{
	util_get_PAGINATION($json , $offset , $limit) ;
	
    $search_text = get_jsonValue($json,"search_text") ; // 字符串 | 关键词
    
    $ret['status'] = -1 ;
	$ret['message'] = "未完成" ;
	return $ret ;
}
//  用户足迹
function user_log($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	util_get_PAGINATION($json , $offset , $limit) ;
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$tm = 30 * 24 * 60 * 60 ;
	$allcount = sql_fetch_one_cell("select count(id) from log_user_footprint where `uid` = $uid and `del` = 0 and `calltime` + $tm > unix_timestamp()") ;	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$userlogs = sql_fetch_rows("select * from log_user_footprint where `uid` = $uid and `del` = 0 and `calltime` + $tm > unix_timestamp() order by `calltime` desc  limit $start , $limit ") ;
	
	$ret['data']['item'] = array() ;
	foreach($userlogs as $u)
	{
		$a = array() ;
		util_set_LOG($a , $u) ;
		$ret['data']['item'][] = $a ;
	}
		
	return $ret ;
}	

// 绑定手机号
function binding_MobilePhone($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$phoneNum = get_jsonValue($json,"phoneNum") ;
	$sms_token = get_jsonValue($json,"sms_token") ;
	$password = get_jsonValue($json,"password") ;
	
	sql_query("delete from `sys_token` where `exceed_at` < unix_timestamp()") ;		// 删除过期时间的token
	if (sql_check("select * from `sys_token` where `tel` = '$phoneNum' and `token` = '$sms_token' and `exceed_at` >= unix_timestamp() and `type` = 1 ") == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_CODE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_CODE"] ;
		return $ret ;
	}
	if (sql_check("select * from user_info where `tel` = '$phoneNum'"))
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_TEL_EXIST"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TEL_EXIST"] ;
		return $ret ;
	}
	
	$desg = sql_fetch_one("select * from user_info where `id` = '$uid'") ;
	if (is_array($desg) && array_key_exists("id", $desg)){
		if ($desg['tel'] == $phoneNum){
			$ret['status'] = $GLOBALS["ERR"]["ERR_TEL_EXIST"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_TEL_EXIST"] ;
			return $ret ;
		}
		
		$pass = substr(md5("gjj" . $password),2,18) ;
		sql_query("update user_info set tel = '$phoneNum' , password_hash = '$pass' where `id` = '$uid'") ;
		
		$token = trim(get_jsonValue($session,"token")) ; 
		
		$ret['data']['session'] = array() ;
		util_set_SESSION($ret['data']['session'] , $token) ;
		$ret['data']['user'] = array() ;
		util_set_USER($ret['data']['user'] , $uid) ;
		return $ret ;	
	}

	$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
	$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
	return $ret ;
}
// 解绑手机号
function unbinding_MobilePhone($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$phoneNum = get_jsonValue($json,"phoneNum") ;
	$sms_token = get_jsonValue($json,"sms_token") ;
	
	sql_query("delete from `sys_token` where `exceed_at` < unix_timestamp()") ;		// 删除过期时间的token
	if (sql_check("select * from `sys_token` where `tel` = '$phoneNum' and `token` = '$sms_token' and `exceed_at` >= unix_timestamp() and `type` = 1 ") == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_CODE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_CODE"] ;
		return $ret ;
	}
	
	
	$desg = sql_fetch_one("select * from user_info where `id` = '$uid'") ;	
	if (is_array($desg) && array_key_exists("id", $desg)){
		if ($desg['tel'] != $phoneNum){
			$ret['status'] = $GLOBALS["ERR"]["ERR_TEL_NOFIND"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_TEL_NOFIND"] ;
			return $ret ;
		}
		sql_query("update user_info set tel = username , password_hash = username where `id` = '$uid'") ;
		
		$token = trim(get_jsonValue($session,"token")) ; 
		
		$ret['data']['session'] = array() ;
		util_set_SESSION($ret['data']['session'] , $token) ;
		$ret['data']['user'] = array() ;
		util_set_USER($ret['data']['user'] , $uid) ;
		return $ret ;	
	}

	$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
	$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
	return $ret ;
}
// 获取启动图
function launch_Image($json)
{
	$ret = array() ;
	$ret['data'] = array() ;
	
	
	$info = sql_fetch_one("select * from prf_config where config_attribute = 'start_display_image' and description = '1'") ;
	if (is_array($info) && array_key_exists("config_attribute" , $info))
	{
		$img = array() ;
		util_set_IMAGE_one($img , $info['config_value']) ;
		$ret['data']['launchImage'] = $img['url'] ;
		return $ret ;
	}

	$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
	$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
	return $ret ;
}
// 查询用户是否有邀请码
function query_invitecode($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$uf = sql_fetch_one("select * from `user_invite` where `uid` = '$uid'") ;
	if (is_null($uf) == true || is_array($uf) == false)
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_INVITE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_INVITE"] ;
		return $ret ;
	}
	
	if (strlen(trim($uf['invite'])) > 0){
		$ret['data']['isInvitecode'] = 1 ;
	}else{
		$ret['data']['isInvitecode'] = 0 ;
	}
	return $ret ;
}

// 返点详情
function rebate_details($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$sql = "select count(uid) from
			((SELECT uid,take_time tm ,take_money at ,-5 ty FROM `pay_rebate_usertake` where uid='$uid' and `state` != 3 ORDER BY take_time desc ) UNION 
			(SELECT uid,create_time tm  ,rebate_money at ,`level` ty FROM `pay_trade_rebate`  where uid='$uid'  and `state` != 2 ORDER BY create_time desc)) tab 
			ORDER BY tab.tm DESC; " ;
		
	
	util_get_PAGINATION($json , $offset , $limit) ;
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell($sql) ;	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	
	$alluser_take = sql_fetch_one_cell("select sum(`rebate_money`) from `pay_trade_rebate` where `uid` = '$uid' and `state` != 2") ;
	$take_money = sql_fetch_one_cell("select sum(`take_money`) from `pay_rebate_usertake` where `uid` = '$uid'  and `state` != 3") ;
	
	$ret['data']['rebateInfo'] = array() ;
	$ret['data']['rebateInfo']['available'] = $alluser_take -  $take_money ;
	$ret['data']['rebateInfo']['cashback'] = $take_money ;
	$ret['data']['rebateInfo']['rebate_list'] = array() ;
	
	$sql = "select tab.uid , tab.tm , tab.at , tab.ty , tab.no , tab.st , tab.apply_t , tab.ok_t , tab.err_t from
			((SELECT uid,take_time tm , apply_time apply_t ,ok_time ok_t , `err_time` err_t ,take_money at ,-5 ty , '' no , `state` st  FROM `pay_rebate_usertake` where uid='$uid' and `state` != 3 ORDER BY apply_time desc ) UNION 
			(SELECT uid,create_time tm  , create_time apply_t ,-1 ok_t , -1 err_t , rebate_money at ,`level` ty , `trade_no` no , `state` st FROM `pay_trade_rebate`  where uid='$uid'  and `state` != 2 ORDER BY create_time desc)) tab 
			ORDER BY tab.apply_t DESC  limit $start , $limit ; " ;
			
	$rebate = sql_fetch_rows($sql) ;
	
	foreach($rebate as $r)
	{
		$a = array() ;
		util_set_REBATE_DETAIL($a , $r) ;
		$ret['data']['rebateInfo']['rebate_list'][] = $a ;
	}
	return $ret ;
}

// 返点详情
function rebate_details_V2($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$sql = "select count(uid) from
			((SELECT uid,take_time tm ,take_money at ,-5 ty FROM `pay_rebate_usertake` where uid='$uid' and `state` != 3 ORDER BY take_time desc ) UNION 
			(SELECT uid,create_time tm  ,rebate_money at ,`level` ty FROM `pay_trade_rebate`  where uid='$uid'  and `state` != 2 and rebate_money>0 ORDER BY create_time desc)) tab 
			ORDER BY tab.tm DESC; " ;
		
	
	util_get_PAGINATION($json , $offset , $limit) ;
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell($sql) ;	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	
	$alluser_take = sql_fetch_one_cell("select sum(`rebate_money`) from `pay_trade_rebate` where `uid` = '$uid' and `state` != 2") ;
	$take_money = sql_fetch_one_cell("select sum(`take_money`) from `pay_rebate_usertake` where `uid` = '$uid'  and `state` != 3") ;
	
	$ret['data']['rebateInfo'] = array() ;
	$ret['data']['rebateInfo']['available'] = $alluser_take -  $take_money ;
	$ret['data']['rebateInfo']['cashback'] = $take_money ;
	$ret['data']['rebateInfo']['rebate_list'] = array() ;
	
	$sql = "select tab.uid , tab.tm , tab.at , tab.ty , tab.no , tab.st , tab.apply_t , tab.ok_t , tab.err_t from
			((SELECT uid,take_time tm , apply_time apply_t ,ok_time ok_t , `err_time` err_t ,take_money at ,-5 ty , '' no , `state` st  FROM `pay_rebate_usertake` where uid='$uid' and `state` != 3 ORDER BY apply_time desc ) UNION 
			(SELECT uid,create_time tm  , create_time apply_t ,-1 ok_t , -1 err_t , rebate_money at ,`level` ty , `trade_no` no , `state` st FROM `pay_trade_rebate`  where uid='$uid'  and `state` != 2 and rebate_money>0 ORDER BY create_time desc)) tab 
			ORDER BY tab.apply_t DESC  limit $start , $limit ; " ;
			
	$rebate = sql_fetch_rows($sql) ;
	
	foreach($rebate as $r)
	{
		$a = array() ;
		util_set_REBATE_DETAIL_V2($a , $r) ;
		$ret['data']['rebateInfo']['rebate_list'][] = $a ;
	}
	return $ret ;
}



// 	积分详情
function integral_details($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	$type = get_jsonValue($json,"type") ;	
	$addsql = "" ;
	if ($type == 1){
		$addsql = " and `score` > 0 " ;
	}elseif ($type == 2){
		$addsql = " and `score` < 0 " ;
	}
	$sql = "select count(`id`) from `log_user_score` where uid = $uid   $addsql" ;
	
	
	util_get_PAGINATION($json , $offset , $limit) ;
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell($sql) ;	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	
	$ret['data']['integralInfo'] = array() ;
	$ret['data']['integralInfo']['available'] = sql_fetch_one_cell("select sum(`score`) from `log_user_score` where uid = $uid") ;
	$ret['data']['integralInfo']['integral_list'] = array() ;
	
	$sql = "select * from `log_user_score` where uid = $uid   $addsql order by `time` desc limit $start , $limit  " ;	
	$rebate = sql_fetch_rows($sql) ;
	
	foreach($rebate as $r)
	{
		$a = array() ;
		util_set_INTEGRAL_DETAIL($a , $r) ;
		$ret['data']['integralInfo']['integral_list'][] = $a ;
	}
	return $ret ;
}
// 用户足迹删除
function user_log_delete($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}

	$id = get_jsonValue($json,"log_id") ;	
	
	sql_query("update `log_user_footprint` set `del` = 1 where `id` = '$id'") ;
	return $ret ;
}

// 评论列表
function comment_list($json , $session)
{
	$ret = array() ;
	$ret['data'] = array() ;

	$content_type = get_jsonValue($json,"content_type") ;	
	$content_id =  get_jsonValue($json,"content_id") ;
	
	
	$sql = "select count(`id`) from `user_comment` where content_type = '$content_type' and content_id = '$content_id' and comment_id = 0" ;	
	$allcount = sql_fetch_one_cell($sql) ;	
	
	util_get_PAGINATION($json , $offset , $limit) ;
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	
	$ret['data']['lists'] = array() ;		// 评论数据
	$list = sql_fetch_rows("select * from `user_comment` where content_type = '$content_type' and content_id = '$content_id' and comment_id = 0 order by send_time desc limit $start  , $limit ") ;
	
	foreach($list as $t){
		$a = array() ;
		util_set_COMMENT($a , $t) ;
		$ret['data']['lists'][] = $a ;
	}
	
	return $ret ;
}

// 添加评论
function comment_add($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$content_type = get_jsonValue($json,"content_type") ;	
	$content_id =  intval(get_jsonValue($json,"content_id")) ;
	$comment_id = intval(get_jsonValue($json,"comment_id")) ;	
	$comment =  get_jsonValue($json,"comment") ;
	
	$exist = false ;
	if ($comment_id > 0){
		$plinfo = sql_fetch_one("select * from `user_comment` where `id` = '$comment_id'") ;
		if (is_null($plinfo) == false && array_key_exists( 'id' , $plinfo)){
			$exist = true ;
		}
	}
	
	if ($exist == true){
		// 存在 , 回复
		sql_insert("INSERT INTO `user_comment` (`content_type`, `content_id`, `send_uid`, `send_time`, `comment_id`, `comment`) VALUES ('$content_type', '$content_id', '$uid',unix_timestamp(), '$comment_id', '$comment')") ;
	}else{
		// 不存在,新的评论
		sql_insert("INSERT INTO `user_comment` (`content_type`, `content_id`, `send_uid`, `send_time`, `comment_id`, `comment`) VALUES ('$content_type', '$content_id', '$uid',unix_timestamp(), '0', '$comment')") ;		
	}
	
	return $ret ;
}

// 删除评论
function comment_del($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$id = get_jsonValue($json,"id") ;	
	
	sql_query("DELETE FROM `user_comment` WHERE (`id`='$id')") ;
	
	return $ret ;
}
// 评论点赞
function comment_like($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$id = get_jsonValue($json,"id") ;	
	
	$info = sql_fetch_one("select * from user_comment_like where `uid` = '$uid' and `comment_id` = '$id'") ;
	if (is_null($info) == false && array_key_exists('uid' , $info)){
		if (intval($info['count']) >= 1){
			$ret['status'] = "-99" ;
			$ret['message'] = "您已经点过赞了" ;
			return $ret ;
		}
	}
	sql_query("INSERT INTO `user_comment_like` (`uid`, `comment_id`, `count`) VALUES ('$uid', '$id', '1')  on duplicate key update `count` = `count` + 1") ;
	sql_query("update `user_comment` set `like` = `like` + 1 where `id` = '$id'") ;	
	return $ret ;
}

        
// 获取超级设计师有效期
function get_designer_time($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$tmend = 30 * 24 * 60 * 60 ;
	$info = sql_fetch_one("select * , from_unixtime(start_time,'%Y-%m-%d') as START , from_unixtime(end_time,'%Y-%m-%d') as END , from_unixtime(end_time - $tmend,'%Y-%m-%d') as TX from user_pay_vip where `user_id` = '$uid' and ( `user_type_class` = '5' or `user_type_class` = '1'  )") ;
	if (is_null($info) == false && array_key_exists('user_id' , $info)){
		$ret['data']['start_time'] = $info['START'] ;
		$ret['data']['end_time'] =  $info['END'] ;
		$ret['data']['notice_time'] =  $info['TX'] ;
		return $ret ;
	}
	
	$ret['status'] = "-99" ;
	$ret['message'] = "您不是超级设计师" ;
	return $ret ;	
}
       
// 点击上传签到日期
function sign_time($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	// 签到,记录连续签到日期
	$connday = 1 ;
	$info = sql_fetch_one("select * , unix_timestamp(curdate()) NowDay from `user_sign_continuity` where `uid` = $uid") ;	
	if (is_null($info) == false && array_key_exists('uid' , $info)){
		$step_tm = intval($info['NowDay']) - intval($info['sign_time']) ; 
		if ($step_tm < 86400){
			// 小于1天
			$ret['status'] = "-1" ;
			$ret['message'] = "不能重复签到" ;
			return $ret ;
		}elseif ($step_tm > 86400){
			// 超过1天
			$connday = 1 ;
		}else{
			// 间隔1天,可以连续
			$connday = intval($info['continuity_count']) + 1 ;
		}
		sql_query("UPDATE `user_sign_continuity` SET `sign_time`= unix_timestamp(curdate()) , `continuity_count`='$connday' WHERE (`uid`='$uid')") ;
	}else{
		sql_query("INSERT INTO `user_sign_continuity` (`uid`, `sign_time`, `continuity_count`) VALUES ('$uid', unix_timestamp(curdate()), '1')") ;
	}
	// 当月时间,时间为当前月1号0点的时间
	$now_money_tm = intval(sql_fetch_one_cell("select unix_timestamp(from_unixtime(unix_timestamp(curdate()),'%Y-%m-01')) as M_DT")) ;	// 获取本月第一天
	$now_money_day = intval(sql_fetch_one_cell("select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) as M_DY ")) ; // 获取当前月的天数
	$now_day = intval(sql_fetch_one_cell("select from_unixtime(unix_timestamp(curdate()),'%d')")) ;	// 今天是那一日
	
	// 签到
	$info = sql_fetch_one("select * from `user_sign_month` where `uid` = '$uid' and `time_month` = '$now_money_tm'") ;
	if (is_null($info) == false && array_key_exists('uid' , $info)){
		$day_sign = $info['day_sign'] ;
		$arr_sign = explode(",",$day_sign);
		$arr_sign[$now_day - 1] = 1 ;
		
		$day_str = "" ; $dh = "" ;
		for($i = 0 ; $i < count($arr_sign) ; ++ $i){
			$day_str .= $dh . $arr_sign[$i] ;
			$dh = "," ;
		}
		sql_query("UPDATE `user_sign_month` SET day_sign = '$day_str' where `uid` = '$uid' and `time_month` = '$now_money_tm'") ;
	}else{
		$day_str = "" ; $dh = "" ;
		for($i = 1 ; $i <= $now_money_day ; ++ $i){
			if ($now_day == $i){
				$day_str .= $dh . "1" ;
			}else{
				$day_str .= $dh . "0" ;
			}
			$dh = "," ;
		}
		// 插入当月信息
		sql_query("INSERT INTO `user_sign_month` (`uid`, `time_month`, `all_day`, `day_sign`) VALUES ('$uid', '$now_money_tm', '$now_money_day', '$day_str')") ;
	}
	
	// 产生签到积分
	$score_add = 0 ;
	if ($connday < 7){
		$score = intval(sql_fetch_one_cell("select `config_value` from  `prf_config` where `config_attribute` = 'sign_point' and `description` = 1")) ;
		$score_add = $score ;
	}elseif ($connday < 30){
		$score = intval(sql_fetch_one_cell("select `config_value` from  `prf_config` where `config_attribute` = 'sign_week_point' and `description` = 1")) ;
		if ($score > 0){
			$score_add = $score ;
		}
	}else{
		$score = intval(sql_fetch_one_cell("select `config_value` from  `prf_config` where `config_attribute` = 'sign_month_point' and `description` = 1")) ;
		if ($score > 0){
			$score_add = $score ;
		}
	}
	if ($score_add > 0)
	{
		$memo = "签到 $connday 天" ;
        addUserScore($uid , $score_add , 2 ,$memo) ;
	}
	//lwx 增加  vip用户延长1天
	$VIP = sql_fetch_one("select *  from user_pay_vip where user_id = $uid") ;
	if($VIP){
	    $end_time=$VIP['end_time']+24*3600;
	    sql_query("UPDATE user_pay_vip  SET `end_time`= $end_time   WHERE (`user_id`='$uid')") ;
	}
	return $ret ;	
}
// 签到
function get_calendar_time($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$ret['data']['calendar'] = array() ;
	
	$now_money_tm = intval(sql_fetch_one_cell("select unix_timestamp(from_unixtime(unix_timestamp(curdate()),'%Y-%m-01')) as M_DT")) ;	// 获取本月第一天
	
	$info = sql_fetch_one("select * , curdate() as DATA_STR , date_format(from_unixtime(time_month),'%w') WK from `user_sign_month` where `uid` = '$uid' and `time_month` = '$now_money_tm'") ;
	if (is_null($info) == false && array_key_exists('uid' , $info)){
		util_set_CALENDAR($ret['data']['calendar'] ,$info ) ;
	}else{
		$now_money_day = intval(sql_fetch_one_cell("select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) as M_DY ")) ; // 获取当前月的天数
		
		$day_str = "" ; $dh = "" ;
		for($i = 1 ; $i <= $now_money_day ; ++ $i){
			$day_str .= $dh . "0" ;
			$dh = "," ;
		}
		// 插入当月信息
		sql_query("INSERT INTO `user_sign_month` (`uid`, `time_month`, `all_day`, `day_sign`) VALUES ('$uid', '$now_money_tm', '$now_money_day', '$day_str')") ;
		
		$info = sql_fetch_one("select * , curdate() as DATA_STR , date_format(from_unixtime(time_month),'%w') WK from `user_sign_month` where `uid` = '$uid' and `time_month` = '$now_money_tm'") ;
		if (is_null($info) == false && array_key_exists('uid' , $info)){
			util_set_CALENDAR($ret['data']['calendar'] ,$info ) ;
		}
	}
	return $ret ;
}

//  奖励提现
function reward_cash($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$cash = get_jsonValue($json , "cash") ;	// | 数值 | 取现金额
    $type = get_jsonValue($json , "type") ;	// | 数值 | 取现方式 （0->支付宝 1->微信）
    $aliAccount  = get_jsonValue($json , "aliAccount") ;	// | 字符串 | 支付宝账号
    $wxOpenid	 = get_jsonValue($json , "wxOpenid") ;	// | 字符串 | 微信openid
    $wxNickName	 = get_jsonValue($json , "wxNickName") ;	// | 字符串 | 微信昵称
    $alluser_take = sql_fetch_one_cell("select sum(`rebate_money`) from `pay_trade_rebate` where `uid` = '$uid' and `state` != 2") ;
	$take_money = sql_fetch_one_cell("select sum(`take_money`) from `pay_rebate_usertake` where `uid` = '$uid'  and `state` != 3") ;
	$available = $alluser_take -  $take_money ;	// 最多可以取现
    if (floatval($cash) > floatval($available)){
    	$ret['status'] = -2 ;
		$ret['message'] = "超出最大提现金额" ;
		return $ret ;
    }
     if(intval($type)==0){
        $wxOpenid="";
        $wxNickName="";
        // 支付宝
        sql_query("INSERT INTO `user_account` (`uid`, `aliAccount` , `default`) VALUES ('$uid', '$aliAccount' , '0') ON DUPLICATE KEY UPDATE `aliAccount` = '$aliAccount' , `default` = 0") ;
        
    }else{
        $aliAccount="";
        // 微信
        sql_query("INSERT INTO `user_account` (`uid`, `wxOpenid`, `wxNickName` , `default`) VALUES ('$uid', '$wxOpenid', '$wxNickName' , '1')  ON DUPLICATE KEY UPDATE `wxOpenid` = '$wxOpenid', `wxNickName` = '$wxNickName' , `default` = 1") ;
    }  
    sql_query("INSERT INTO `pay_rebate_usertake` (`uid`, `take_money`, `state`, `apply_time`, `aliAccount` , `wxOpenid` , `wxNickName` , `memo`) VALUES ('$uid', '$cash', '0', unix_timestamp(), '$aliAccount' , '$wxOpenid' , '$wxNickName' , '取现')") ;
    
    return $ret ;
}
// 获取是否签到信息(签到入口)
function get_sign_info($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}

	$ret['data']['sign'] = 0 ;
	$info = sql_fetch_one("select * , unix_timestamp(curdate()) NowDay from `user_sign_continuity` where `uid` = $uid") ;	
	if (is_array($info) == true && array_key_exists('uid' , $info)){
		$step_tm = intval($info['NowDay']) - intval($info['sign_time']) ; 
		if ($step_tm < 86400){
			$ret['data']['sign'] = 1 ;
		}
	}
	return $ret ;
}

/**
 * 搜索 * 
 */
function _search_space($nm)	// 空间
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `grp_space` where $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}
function _search_color($nm)		// 色系
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `grp_color` where $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}
function _search_style($nm)		// 风格
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `grp_style` where $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}
function _search_class($nm)		// 类别/分类
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `grp_class` where $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}
function _search_brand($nm)		// 品牌
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `grp_brand` where `useing` = 1 and $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}
function _search_place($nm)		// 产地
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `grp_place` where $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}
function _search_fabric($nm)		// 面料
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `base_sys_fabric` where $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}
function _search_material($nm)		// 材质
{
	$lk1 = _get_search_like($nm , "name") ;
	$list = sql_fetch_rows("SELECT `id` FROM `base_sys_material` where $lk1") ;
	$rntstr = "" ; $dh = "" ;
	
	if (is_array($list))	foreach($list as $a){
		$rntstr .= $dh . $a['id'] ;		$dh = " , " ;
	}
	return $rntstr ;
}

function _get_search_like($query , $content)
{
	$lk1 = "`$content` like \"%$query%\"" ;
	return " ( $lk1 ) " ;
}

/**
 * 搜索 -- 正在用的
 * @param $json
 * @return array
 */
function search($json)
{ 
    $query 	= trim(get_jsonValue($json , "query")) ;	// | 字符串 | 搜索内容
    $type 	= trim(get_jsonValue($json , "type")) ;	//  | 字符串 | 类型
    $query_int = intval($query) ;   // 搜索内容是整形
    $arr_qu = explode(" ",$query);
    
    //type 类型：
    //* set 套装
    //* item 单品
    //* show 设计家 -- subtype 起作用 
    
    $ret = array() ;
	$ret['data'] = array() ;
	
	
    $sql_from = "" ;
    if ($type == "show"){
	    $addwhere = "" ;
		// 子类匹配搜索
	    $subtype = get_jsonValue($json , "subtype") ;	//  | id（数值） 数组 | 子类型，同 showlist_v2 的 idArray    	
    	if (is_array($subtype) && count($subtype) > 0){
			$addwhere = " and `state` in (" ;
			$dh = "" ;
			foreach($subtype as $id){
				if ($id == 5){
					$addwhere .= $dh . " 0 " ;	$dh = "," ;
				}else{
					$addwhere .= $dh . " $id " ;	$dh = "," ;
				}
			}
			$addwhere .= ")" ;
		}
		
		
		
    	$where_set_all = "" ;
    	$where_and = "" ;
    	
    	if (is_array($arr_qu)) for($i = 0 ; $i  < count($arr_qu) ; ++ $i)
    	{
    		$query = $arr_qu[$i] ;
    		if (strlen($query) == 0)	break ;
    	     
			// 表内匹配
			$where_apprnt = "" ;		$isor = "" ;
			if (strlen($query) > 0){
				$q1 = _get_search_like($query , "name") ;
				$q2 = _get_search_like($query , "content") ;
				 
				$where_apprnt = $isor . " ($q1 or $q2) " ;	
				$isor = " or " ;
			}
			$f_space = _search_space($query) ;
			if (st.rlen($f_space) > 0){
				$where_apprnt .= $isor . " `space_id` in ($f_space)" ;
				$isor = " or " ;
			}
			
			// 表外id搜索匹配
			$id_list = "" ; $dh = "" ;		
			$f_color = _search_color($query) ;		//  色系
			if (strlen($f_color) > 0){
				$s_c_l = sql_fetch_rows("select show_id from show_color where color_id in ($f_color) group by show_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					$id_list .= $dh . $s['show_id'] ; $dh = "," ;
				}
			}
			$f_style = _search_style($query) ;		// 风格
			if (strlen($f_style) > 0){
				$addswh = "" ;
				if (strlen($id_list) > 0){
					$addswh = " and `show_id` not in ($id_list) " ;
				}
				
				$s_c_l = sql_fetch_rows("select show_id from show_style where style_id in ($f_style) $addswh group by show_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					$id_list .= $dh . $s['show_id'] ; $dh = "," ;
				}
			}
			
			
			// 组合最终搜索条件
			$addswh = "" ;
			if (strlen($id_list) > 0){
				$addswh = " or `id` in ($id_list) " ;
			}
			
			$where_set_all .= $where_and . " (  $where_apprnt $addswh  ) " ;
			$where_and = " and " ;
    	}
    	$sql_from = " from `show` where `app_return` = 1 $addwhere and ( $where_set_all )" ;
    }elseif($type == "set"){
    	
    	$where_set_all = "" ;
    	$where_and = "" ;
    	
    	if (is_array($arr_qu)) for($i = 0 ;$i  < count($arr_qu) ; ++ $i)
    	{
    		$query = $arr_qu[$i] ;
    		if (strlen($query) == 0)	break ;
    		
	    	// 表内匹配
			$where_apprnt = "" ;		$isor = "" ;
			if (strlen($query) > 0){
				$q1 = _get_search_like($query , "name") ;
				$q2 = _get_search_like($query , "tips") ;
				$where_apprnt = $isor . " ($q1 or $q2) " ;	
				$isor = " or " ;
			}
			$f_space = _search_space($query) ;
			if (strlen($f_space) > 0){
				$where_apprnt .= $isor . " `space_id` in ($f_space)" ;
				$isor = " or " ;
			}
			// 表外id搜索匹配
			$id_list = "" ; $dh = "" ;		
	    	$f_color = _search_color($query) ;		//  色系
			if (strlen($f_color) > 0){
				$s_c_l = sql_fetch_rows("select taozhuang_id from taozhuang_color where color_id in ($f_color) group by taozhuang_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					$id_list .= $dh . $s['taozhuang_id'] ; $dh = "," ;
				}
			}
			$f_style = _search_style($query) ;		// 风格
			if (strlen($f_style) > 0){
				$addswh = "" ;
				if (strlen($id_list) > 0){
					$addswh = " and `taozhuang_id` not in ($id_list) " ;
				}
				
				$s_c_l = sql_fetch_rows("select taozhuang_id from taozhuang_style where style_id in ($f_style) $addswh group by taozhuang_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					$id_list .= $dh . $s['taozhuang_id'] ; $dh = "," ;
				}
			}
			
	    	// 组合最终搜索条件
			$addswh = "" ;
			if (strlen($id_list) > 0){
				$addswh = " or `sid` in ($id_list) " ;
			}
			
			$where_set_all .= $where_and . " (  $where_apprnt $addswh  ) " ;
			$where_and = " and " ;
    	}
    	$sql_from = " from `taozhuang` where `app_return` = 1 and ( $where_set_all )" ;
    }elseif($type == "item"){
    	$where_set_all = "" ;
    	$where_and = "" ;


        // id 匹配
        if ($query_int > 0){
            if (intval(sql_fetch_one_cell("select `id` from sys_item where `id` = '$query_int'")) == $query_int){
                $where_set_all = "`id` = '$query_int'" ;
                $arr_qu = 0 ;
            }
        }


        if (is_array($arr_qu)) for($i = 0 ; $i  < count($arr_qu) ; ++ $i)
    	{
            $query = $arr_qu[$i] ;
    		if (strlen($query) == 0)	break ;

    		
	    	// 表内匹配
			$where_apprnt = "" ;		$isor = "" ;
			if (strlen($query) > 0){
				$q1 = _get_search_like($query , "name") ;
				$q2 = _get_search_like($query , "factory_code") ;
				$where_apprnt = $isor . " ($q1 or $q2) " ;	
				$isor = " or " ;
			}
	    	$f_brand = _search_brand($query) ;		// 品牌
			if (strlen($f_brand) > 0){
				$where_apprnt .= $isor . " `brand_id` in ($f_brand)" ;
				$isor = " or " ;
			}
			$f_class = _search_class($query) ;		// 类别
			if (strlen($f_class) > 0){
				$where_apprnt .= $isor . " `class_id` in ($f_class)" ;
				$isor = " or " ;
			}
			
			// 表外id搜索匹配
			$id_list = "" ; $dh = "" ;		
	    	$f_color = _search_color($query) ;		//  色系
			if (strlen($f_color) > 0){
				$s_c_l = sql_fetch_rows("select item_id from sys_item_color where color_id in ($f_color) group by item_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					if (intval($s['item_id']) > 0){
						$id_list .= $dh . $s['item_id'] ; $dh = "," ;
					}
				}
			}
			$f_style = _search_style($query) ;		// 风格
			if (strlen($f_style) > 0){
				$addswh = "" ;
				if (strlen($id_list) > 0){
					$addswh = " and `item_id` not in ($id_list) " ;
				}
				
				$s_c_l = sql_fetch_rows("select item_id from sys_item_style where style_id in ($f_style) $addswh group by item_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					if (intval($s['item_id']) > 0){
						$id_list .= $dh . $s['item_id'] ; $dh = "," ;
					}
				}
			}
			$f_place = _search_place($query) ;		// 产地
			if (strlen($f_place) > 0){
				$addswh = "" ;
				if (strlen($id_list) > 0){
					$addswh = " and `item_id` not in ($id_list) " ;
				}
				
				$s_c_l = sql_fetch_rows("select item_id from sys_item_place where place_id in ($f_place) $addswh group by item_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					if (intval($s['item_id']) > 0){
						$id_list .= $dh . $s['item_id'] ; $dh = "," ;
					}
				}
			}
			$f_fabric = _search_fabric($query) ;			// 面料
			if (strlen($f_fabric) > 0){
				$addswh = "" ;
				if (strlen($id_list) > 0){
					$addswh = " and `item_id` not in ($id_list) " ;
				}
				
				$s_c_l = sql_fetch_rows("select item_id from base_sys_preview where fid in ($f_fabric) $addswh group by item_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					if (intval($s['item_id']) > 0){
						$id_list .= $dh . $s['item_id'] ; $dh = "," ;
					}
				}
			}
			$f_material= _search_material($query) ;			// 材质
			if (strlen($f_material) > 0){
				$addswh = "" ;
				if (strlen($id_list) > 0){
					$addswh = " and `item_id` not in ($id_list) " ;
				}
				
				$s_c_l = sql_fetch_rows("select item_id from base_sys_preview where mid in ($f_material) $addswh group by item_id ") ;
				if (is_array($s_c_l)) foreach($s_c_l as $s){
					if (intval($s['item_id']) > 0){
						$id_list .= $dh . $s['item_id'] ; $dh = "," ;
					}
				}
			}
			
	    	// 组合最终搜索条件
			$addswh = "" ;
			if (strlen($id_list) > 0){
				$addswh = " or `id` in ($id_list) " ;
			}
		
			$where_set_all .= $where_and . " (  $where_apprnt $addswh  ) " ;
			$where_and = " and " ;
    	}
		
		//20181026 新增品牌的单品在配置的城市显示 -start
		$city_filter = trim(get_jsonValue($json,"cityname")) ;
		$addwhere = "";
		if (strlen($city_filter) >= 2){
			$brand_lists = array() ;
			$brand_lists = sql_fetch_rows("select b.* from grp_brand_city b inner join grp_city c on b.city_id = c.id inner join grp_brand d on b.brand_id = d.id and d.`useing` = 1 where c.`name` = '$city_filter'") ;
			$brands = "" ; $dh = "" ;
			foreach($brand_lists as $b){
				$brands .= $dh.$b['brand_id'] ; $dh = "," ;
			}
			//if ($brands != "")
			{
				$other_list = sql_fetch_rows("SELECT * from grp_brand a where  a.`useing` = 1 and  a.id not in (select e.brand_id from grp_brand_city e )") ;
				foreach($other_list as $b){
					$brands .= $dh.$b['id'] ; $dh = "," ;
				}

				$addwhere = " and `brand_id` in ($brands) " ;	
			}
		}		
		//-end		
		
		
    	$sql_from = " from `sys_item` where `app_return` = 1 $addwhere and ( $where_set_all )" ;
    }else{
    	$ret['status'] = "-100" ;
		$ret['message'] = "类型错误" ;
		return $ret ;
    }
    
    
    
    $sql = "select count(*) $sql_from" ;	
	$allcount = sql_fetch_one_cell($sql) ;	
	
	util_get_PAGINATION($json , $offset , $limit) ;
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	
	$ret['data']['item'] = array() ;
	$bitem = sql_fetch_rows("select * $sql_from limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		if ($type == "show"){
			util_set_LOG_search_for_show($rn , $b) ;
		}elseif ($type == "set"){
			util_set_LOG_search_for_set($rn , $b) ;
		}elseif ($type == "item"){
			util_set_LOG_search_for_item($rn , $b) ;
		}
		$ret['data']['item'][] = $rn ;
	}

	return $ret ;	
}
// 搜索热榜
function search_hotlist($json)
{
	$ret['status'] = "-100" ;
	$ret['message'] = "接口尚未完成" ;
	return $ret ;
}
// 搜索提示
function search_tips($json)
{
	$query 	= trim(get_jsonValue($json , "query")) ;	// | 字符串 | 搜索内容
    $type 	= trim(get_jsonValue($json , "type")) ;	//  | 字符串 | 类型
    //type 类型：         
    //* set 套装
    //* item 单品
    //* show 设计家 -- subtype 起作用 
    
    $ret = array() ;
	$ret['data'] = array() ;
	
	
    $sql_from = "" ;
    if ($type == "show"){
	    $addwhere = "" ;
		// 子类匹配搜索
	    $subtype = get_jsonValue($json , "subtype") ;	//  | id（数值） 数组 | 子类型，同 showlist_v2 的 idArray    	
    	if (is_array($subtype) && count($subtype) > 0){
			$addwhere = " and `state` in (" ;
			$dh = "" ;
			foreach($subtype as $id){
				if ($id == 5){
					$addwhere .= $dh . " 0 " ;	$dh = "," ;
				}else{
					$addwhere .= $dh . " $id " ;	$dh = "," ;
				}
			}
			$addwhere .= ")" ;
		}
		
		$q1 = _get_search_like($query , "name") ;
		
		$list = sql_fetch_rows("SELECT `name` FROM `show` where `app_return` = 1  $addwhere  and  $q1") ;
				$ret['data']['item'] = array() ;
		if (is_array($list))	foreach($list as $a){
			$ret['data']['item'][] = $a['name'] ;
		}
		return $ret ;
    }elseif($type == "set"){
    	$q1 = _get_search_like($query , "name") ;
    	$list = sql_fetch_rows("SELECT `name` FROM `taozhuang` where `app_return` = 1 and $q1") ;
		$ret['data']['item'] = array() ;
		if (is_array($list))	foreach($list as $a){
			$ret['data']['item'][] = $a['name'] ;
		}
		return $ret ;
    }elseif($type == "item"){
    	// 表内匹配
    	$q1 = _get_search_like($query , "name") ;
    	$list = sql_fetch_rows("SELECT `name` FROM `grp_class` where $q1") ;
		$ret['data']['item'] = array() ;
		if (is_array($list))	foreach($list as $a){
			$ret['data']['item'][] = $a['name'] ;
		}
		return $ret ;
    }else{
    	$ret['status'] = "-100" ;
		$ret['message'] = "类型错误" ;
		return $ret ;
    }
}
// 用户类型
function get_userType($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$uinfo = sql_fetch_one("select * from user_info where id = '$uid'") ;
	if (is_null($uinfo) || array_key_exists("password_hash" , $uinfo) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}

	$ret['data']['type'] = $uinfo['user_type'] ;
	$ret['data']['typeClass'] = $uinfo['user_type_class'] ;
	
	return $ret ;	
}
//提现账户
function pickCash_account($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}

	$ret['data']['pickAccount'] = array() ;

	$acc = sql_fetch_one("select * from user_account where `uid` = '$uid'") ;
	if (is_array($acc) == true){
		$ret['data']['pickAccount']['type'] = $acc['default'] ;
		$ret['data']['pickAccount']['aliAccount'] = $acc['aliAccount'] ;
		$ret['data']['pickAccount']['wxOpenid'] = $acc['wxOpenid'] ;
		$ret['data']['pickAccount']['wxNickName'] = $acc['wxNickName'] ;
	}else{
		$info = sql_fetch_one("select * from pay_rebate_usertake where `uid` = '$uid' order by `apply_time` desc limit 1") ;
		if (is_null($info) || is_array($info) == false){
			$ret['data']['pickAccount']['type'] = 0 ;
			$ret['data']['pickAccount']['aliAccount'] = "" ;
			$ret['data']['pickAccount']['wxOpenid'] = "" ;
			$ret['data']['pickAccount']['wxNickName'] = "" ;
		}else{
			$paytype = 1 ;
			if (strlen($info['aliAccount']) > 0){
				$paytype = 0 ;
			}
			$ret['data']['pickAccount']['type'] = $paytype ;
			$ret['data']['pickAccount']['aliAccount'] = $info['aliAccount'] ;
			$ret['data']['pickAccount']['wxOpenid'] = $info['wxOpenid'] ;
			$ret['data']['pickAccount']['wxNickName'] = $info['wxNickName'] ;
		}
	}
	
	return $ret ;	
}

// 新疆用套装列表
function xj_setlist($json , $session)
{
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	
	$addwhere = "" ;
	
	$tzid 	= intval(get_jsonValue($filter , "tzid")) ;  //| 字符串 | 套装id
    $space 	= get_jsonValue($filter , "space") ; //| 字符串 | 空间 （客厅、餐厅、卧室、书房、儿童房、厨房、卫生间）
    $style 	= get_jsonValue($filter , "style") ; //| 字符串 | 风格 （现代、北欧、美式现代、美式休闲、美式传统、欧式现代、欧式休闲、欧式传统、新中式、后现代）
    $color 	= get_jsonValue($filter , "color") ; //| 字符串 | 色系 （黑白、灰色、米色、金属色、红色、橙色、绿色、蓝色、紫色、黄色、咖啡色、彩色）
    $brand 	= get_jsonValue($filter , "brand") ; //| 字符串 | 品牌 （0：RCD，1：完整家居，2：绿城，3：贵阳宽城国际，4：花样年华）
		
	if ($tzid > 0){
		$addwhere .= " and `sid` = '$tzid'" ;
	}else{
		if (strlen($space) > 0){
			$addwhere .= _filter_room_set($space) ;		// 空间
		}
		if (strlen($style) > 0){
			$addwhere .= _filter_style_set($style) ;	// 风格
		}
		if (strlen($color) > 0){
			$addwhere .= _filter_color_set($color) ;	// 色系
		}
		if (strlen($brand) > 0){
			$addwhere .= " and `state_tree` = $brand " ;	// 品牌
		}
	}

	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(sid) from taozhuang where `app_return` = 1 $addwhere") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	

	$bitem = sql_fetch_rows("select * from taozhuang where `app_return` = 1 $addwhere order by `top_time` desc , `sid` desc limit $start , $limit") ;	
	foreach ($bitem as $b){
		$rn = array() ;
		util_set_XJ_SET_SIMPLE($rn , $b) ;
		$ret['data']['set'][] = $rn ;
	}

	return $ret ;	
}

// 新疆套装详情
function xj_set($json , $session)
{
	$id = get_jsonValue($json,"tzid") ;

	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	$item = sql_fetch_one("select * from taozhuang where `sid` = '$id'") ;
	if (is_null($item) || array_key_exists('sid' , $item) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NO_DATA"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NO_DATA"] ;
		return $ret ;
	}
	
	util_set_XJ_SET_DETAIL($ret['data']['set'] , $item) ;
	
	return $ret ;
}



//#### 3.2.87 `arealist_v2` 城市县区列表2
function arealist_v2()
{
	// 获取缓冲数据
	$cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
	$ret = mem_getValues($cache_key) ;
	if ($ret != null){
		return $ret ;
	}
	
	$ret = array() ;
	$ret['data'] = array() ;	
	$ret['data']['area'] = array() ;
	
	$bitem = sql_fetch_rows("select * from grp_city") ;
	util_set_AREA2($ret['data']['area'] , $bitem) ;
	
	// 设置缓冲数据
	mem_setKey($cache_key, $ret) ;
	return $ret ;	
}


//#### 3.2.88 `item_exchange_List` 积分兑换列表
function item_exchange_List($json, $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$ret = array() ;
	$ret['data'] = array() ;	
	$ret['data']['item'] = array() ;		//  item | `ITEM_INTEGRAL`数组 | 兑换单品列表
	
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(*) from  prf_score_exchange where unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) >= unix_timestamp(from_unixtime(`start_time`,'%Y-%m-%d')) and (`end_time` = 0 or unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) <= unix_timestamp(from_unixtime(`end_time`,'%Y-%m-%d')))") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
		
	$lists = sql_fetch_rows("select * , from_unixtime(`start_time`,'%Y-%m-%d') as StartDay , from_unixtime(`end_time`,'%Y-%m-%d') as EndDay from prf_score_exchange where unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) >= unix_timestamp(from_unixtime(`start_time`,'%Y-%m-%d')) and (`end_time` = 0 or unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) <= unix_timestamp(from_unixtime(`end_time`,'%Y-%m-%d')))  limit $start , $limit") ;
	if (is_array($lists)) foreach($lists as $l){
		$aa = array() ;
		util_set_ITEM_INTEGRAL($aa , $l) ;
		$ret['data']['item'][] = $aa ;
	}
	
	$u_a = sql_fetch_one("select * from user_address where `uid` = '$uid' order by `type` desc") ;
	$ret['data']['address'] = (object)array() ;
	if (is_array($u_a) == true && array_key_exists('uid' , $u_a) == true){
		$ret['data']['address'] = array() ;
		util_set_ADDRESS($ret['data']['address'] , $u_a) ;
	}
	return $ret ;	
}

//#### 3.2.89 `integral_exchange` 积分兑换
function integral_exchange($json , $session)
{
	$discount_id = get_jsonValue($json,"discount_id") ;	// id | 数值 | 活动序号
	$number = intval(get_jsonValue($json,"number")) ;			// number | 数值 | 兑换数量（默认为1，可以不传）
	if ($number <= 0) $number = 1 ;
	$address_id = intval(get_jsonValue($json,"address_id")) ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	
	
	$ret = array() ;
	$ret['data'] = array() ;

	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	
	$one = sql_fetch_one("select * from prf_score_exchange where `id` = '$discount_id' and unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) >= unix_timestamp(from_unixtime(`start_time`,'%Y-%m-%d')) and (`end_time` = 0 or unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) <= unix_timestamp(from_unixtime(`end_time`,'%Y-%m-%d')))") ;
	if (is_array($one) == false || array_key_exists('id' , $one) == false)
	{
		$ret['status'] = -1 ;
		$ret['message'] = "活动已经结束,无法兑换" ;
		return $ret ;
	}
	
	if ($number > $one['last_total'] && intval($one['total']) > 0)
	{
		$ret['status'] = -2 ;
		$ret['message'] = "已经没有库存了" ;
		return $ret ;
	}
	if (intval($one['user_max_number']) > 0){
		// 判定用户最多兑换数目
		$user_ex_num = intval(sql_fetch_one_cell("select sum(p.`number`) from `pay_score_item` p inner join `log_user_score` l on p.`log_id` = l.`id` and l.`uid` = '$uid'  where p.`ex_id` = '$discount_id' and  p.`logistics_state` <= 2")) ;
		if ($user_ex_num >= intval($one['user_max_number'])){
			$ret['status'] = -4 ;
			$ret['message'] = "您最多可以兑换 " . $one['user_max_number'] . " 件" ;
			return $ret ;
		}
	}
	
	$userall = intval(sql_fetch_one_cell("select sum(`score`) from `log_user_score` where uid = $uid")) ;
	$usescore = $number * $one['integral'] ;
	if ($userall < $usescore)
	{
		$ret['status'] = -3 ;
		$ret['message'] = "金币不足" ;
		return $ret ;
	}
	$item_id = $one['item_id'] ;	
	$u_a = sql_fetch_one("select * from user_address where `uid` = '$uid' and `id` = $address_id") ;
	$address = $u_a['area'] . " " . $u_a['address'] ;
	$mobile = trim($u_a['mobile']) ;
	if (is_array($u_a) == false || array_key_exists('address' , $u_a) == false || strlen($mobile) == 0)
	{
		$ret['status'] = -5 ;
		$ret['message'] = "请先设置收货地址." ;
		return $ret ;
	}
	
	$address = $u_a['area'] . " " . $u_a['address'] ;
	$mobile = $u_a['mobile'] ;

	sql_query("update prf_score_exchange set last_total = last_total - 1 where `id` = '$discount_id'") ;
    $newlogid = addUserScore($uid , - $usescore , 100 ,"兑换单品: {$one['item_id']} ,  $number 个") ;
	sql_insert("INSERT INTO `pay_score_item` (`ex_id`, `log_id` , `item_id`, `number`, `score` , `accept_address`, `accept_photo`) VALUES ('$discount_id', '$newlogid' , '$item_id', '$number', '$usescore', '$address', '$mobile')") ;

	$ret['data']['integrals'] = $userall -  $usescore ;		// integrals | 数值 | 用户总积分	
	return $ret ;	
}

//  优惠券列表
function coupon_list($json , $session)
{
	$ret = array() ;
	$ret['data'] = array() ;

    $uid = util_set_GETUID_for_SESSION($session) ;
    if ($uid <= 0){
        $ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
        $ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
        return $ret ;
    }

	util_get_PAGINATION($json , $offset , $limit) ;


	$type = intval(get_jsonValue($json,"type")) ;     //  类别 （0-->全部优惠券，1-->我的优惠券）
	if ($type == 0)
    {
        $u_type = sql_fetch_one_cell("select `user_type` from user_info where `id` = '$uid'") ;

        $start = ($offset - 1) * $limit ;
        $allcount = sql_fetch_one_cell("select count(*) from `prf_coupon` where `state` = 1 and (`user_types` = '' or `user_types` like '%$u_type%')") ;

        $ret['data']['pagination'] = array() ;
        util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;

        $NowDay_int = "unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) as NowDay_int" ;
        $EndDay_int = "unix_timestamp(from_unixtime(`end_time`,'%Y-%m-%d')) as EndDay_int" ;
        $StartDay = "from_unixtime(`start_time`,'%Y-%m-%d') as StartDay" ;
        $EndDay = "from_unixtime(`end_time`,'%Y-%m-%d') as EndDay" ;

        $coupon_list = sql_fetch_rows("select * , `id` as coupon_id , $NowDay_int , $EndDay_int , $StartDay , $EndDay from `prf_coupon`  where `state` = 1 and (`user_types` = '' or `user_types` like '%$u_type%') order by `id` desc limit $start , $limit ") ;
        $ret['data']['coupon_item'] = array() ;
        if (is_array($coupon_list)) foreach($coupon_list as $l){
            $id = $l['id'] ;
            if (intval(sql_fetch_one_cell("select `uid` from `user_coupon` where `uid` = '$uid' and `coupon_id` = '$id'")) == $uid)
            {
                $l['state'] = 3 ;
            }else{
                $l['state'] = 4 ;
            }
            $l['from_uid'] = 0 ;

            $aa = array() ;
            util_set_CONPON_INFO($aa , $l , $uid) ;
            $ret['data']['coupon_item'][] = $aa ;
        }
        return $ret ;
    }

	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$allcount = sql_fetch_one_cell("select count(*) from `user_coupon` u inner join `prf_coupon` p on u.`coupon_id` = p.`id` where u.`uid` = '$uid' and u.state=0 and p.state= 1") ;
	
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$NowDay_int = "unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) as NowDay_int" ;
	$EndDay_int = "unix_timestamp(from_unixtime(u.`end_time`,'%Y-%m-%d')) as EndDay_int" ;
	$StartDay = "from_unixtime(u.`start_time`,'%Y-%m-%d') as StartDay" ;
	$EndDay = "from_unixtime(u.`end_time`,'%Y-%m-%d') as EndDay" ;
	$coupon_list = sql_fetch_rows("select $NowDay_int , $EndDay_int , $StartDay , $EndDay , u.* , p.`name` , p.`title` , p.`img_id` , p.`excode` , p.`shipping` , p.`user_max_number` , p.`totle_count` , p.`last_count` , p.`coupon_rule`,p.`trans_type`  from `user_coupon` u inner join `prf_coupon` p on u.`coupon_id` = p.`id` where u.`uid` = '$uid' and u.state=0 and p.state=1 order by u.`id` desc limit $start , $limit ") ;
	$ret['data']['coupon_item'] = array() ;
	if (is_array($coupon_list)) foreach($coupon_list as $l){
		$aa = array() ;
		util_set_CONPON_INFO($aa , $l , $uid) ;
		$ret['data']['coupon_item'][] = $aa ;
	}	
	return $ret ;	
}
// 领取优惠券
function get_coupon($json , $session)
{
    $ret = array();
    $ret['data'] = array();

    $uid = util_set_GETUID_for_SESSION($session);
    if ($uid <= 0) {
        $ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"];
        $ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"];
        return $ret;
    }

    $id = intval(get_jsonValue($json,"id")) ;        // id | 数值 | 优惠券号码
    $code = get_jsonValue($json,"code") ;    // code | 字符串 | 兑换码

    $u_type = sql_fetch_one_cell("select `user_type` from user_info where `id` = '$uid'") ;

    $NowDay_int = "unix_timestamp(from_unixtime(unix_timestamp(),'%Y-%m-%d')) as NowDay_int" ;
    $EndDay_int = "unix_timestamp(from_unixtime(`end_time`,'%Y-%m-%d')) as EndDay_int" ;

    $coupon = sql_fetch_one("select * , $NowDay_int , $EndDay_int from prf_coupon where `id` = '$id' and `excode` = '$code' and `state` = 1") ;
    if (is_array($coupon) == false or array_key_exists("id" , $coupon) == false){
        $ret['status'] = -1 ;
        $ret['message'] = "您无法领取此优惠卷";
        return $ret;
    }
    // and (`end_time` = 0 OR unix_timestamp() < `end_time` )
    if ($coupon['end_time'] > 0){
        if ($coupon['NowDay_int'] > $coupon['EndDay_int']){
            $ret['status'] = -10 ;
            $ret['message'] = "此优惠卷已经到期";
            return $ret;
        }
    }
    // and (`user_types` = '' or `user_types` like '%,$u_type,%')
    if ($coupon['user_types'] != ""){
        $typelist = explode(",",$coupon['user_types']) ;
        $exist = false ;
        foreach ($typelist as $ty){
            if (intval($ty) == intval($u_type)){
                $exist = true ;
            }
        }
        if ($exist == false){
            $ret['status'] = -12 ;
            $ret['message'] = "您无法领取此类型的优惠卷";
            return $ret;
        }
    }

    if (intval($coupon['user_max_number']) > 0) {
        $mycount = sql_fetch_one_cell("select count(*) from `user_coupon` WHERE `uid` = '$uid' and `coupon_id` = '$id'");
        if ($mycount >= intval($coupon['user_max_number'])){
            $ret['status'] = -2 ;
            $ret['message'] = "您无法领取更多了";
            return $ret;
        }
    }

    if (intval($coupon['totle_count']) > 0) {
        if (intval($coupon['last_count']) <= 0) {
            $ret['status'] = -3 ;
            $ret['message'] = "已经没有库存了";
            return $ret;
        }
    }

    $s_t = $coupon['start_time'] ;
    $e_t = $coupon['end_time'] ;
    if (intval($coupon['time_state']) > 0){
        $ed_p_tm = intval($coupon['time_state']) ;
        $e_t = sql_fetch_one_cell("select unix_timestamp() + $ed_p_tm") ;
    }
    $price = $coupon['price'] ;
    $newrow = sql_insert("insert into `user_coupon` (`uid` , `coupon_id` , `start_time` , `end_time` , `price` , `action` , `action_id` , `url` ) values ('$uid' , '$id' , '$s_t' , '$e_t' , '$price' , '{$coupon['action']}' , '{$coupon['action_id']}' , '{$coupon['url']}' )") ;
    if ($newrow > 0){
        if (intval($coupon['totle_count']) > 0) {
            sql_query("update prf_coupon set last_count = last_count - 1 where `id` = '$id' ") ;
        }
    }else{
        $ret['status'] = -4 ;
        $ret['message'] = "领取失败";
        return $ret;
    }

    return $ret ;
}

// 转让优惠卷
function transfer_conpon($json , $session)
{
    $ret = array();
    $ret['data'] = array();

    $uid = util_set_GETUID_for_SESSION($session);
    if ($uid <= 0) {
        $ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"];
        $ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"];
        return $ret;
    }

    $user_con_id = intval(get_jsonValue($json,"con_id")) ;        // 优惠卷ID
    $tel = get_jsonValue($json,"tel") ;                     // 手机号

    $conpon = sql_fetch_one("SELECT u.* , p.`trans_type` FROM prf_coupon p INNER JOIN user_coupon u ON u.coupon_id = p.id where u.`id` = $user_con_id and u.uid = $uid") ;
    if ($conpon == null || $conpon['id'] != $user_con_id){
        $ret['status'] = -101 ;
        $ret['message'] = "你没有此优惠卷";
        return $ret ;
    }
    if (strlen($conpon['trans_type']) == 0){
        $ret['status'] = -102 ;
        $ret['message'] = "此优惠卷无法转让";
        return $ret ;
    }
    $ty_list = explode("," , $conpon['trans_type']) ;
    if (count($ty_list) == 0){
        $ret['status'] = -103 ;
        $ret['message'] = "此优惠卷没有转让类型";
        return $ret ;
    }

    $oth_user = sql_fetch_one("select * from `user_info` where `tel` = '$tel'") ;
    if ($oth_user == null || $oth_user['tel'] != $tel){
        $ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"];
        $ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"];
        return $ret;
    }

    $ex = false ;
    foreach ($ty_list as $ty){
        if (intval($ty) == intval($oth_user['user_type'])){
            $ex = true ;
            break ;
        }
    }
    if (!$ex){
        $ret['status'] = -104 ;
        $ret['message'] = "无法转给此类型用户";
        return $ret ;
    }

    $s_t = $conpon['start_time'] ;
    $e_t = $conpon['end_time'] ;
    if (intval($conpon['time_state']) > 0){
        $ed_p_tm = intval($conpon['time_state']) ;
        $e_t = sql_fetch_one_cell("select unix_timestamp() + $ed_p_tm") ;
    }

    $coupon_id = $conpon['coupon_id'] ;
    $newrow = sql_insert("insert into `user_coupon` (`uid` , `coupon_id` , `start_time` , `end_time` , `price` , `action` , `action_id` , `url` , `from_uid`) values ('{$oth_user['id']}' , '$coupon_id' , '$s_t' , '$e_t' , '{$conpon['price']}' , '{$conpon['action']}' , '{$conpon['action_id']}' , '{$conpon['url']}' , '$uid' )") ;
    if ($newrow > 0){
        sql_query("delete from `user_coupon` where `id` = '$user_con_id'") ;
        return $ret ;       // 转让成功
    }
    $ret['status'] = -105 ;
    $ret['message'] = "转让失败";
    return $ret ;
}
// 获取收藏的户型
function get_favourt_building($json , $session)
{
    $ret = array();
    $ret['data'] = array();
    util_get_PAGINATION($json , $offset , $limit) ;
    $uid = util_set_GETUID_for_SESSION($session) ;
    if ($uid <= 0){
        $ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
        $ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
        return $ret ;
    }

    // 组织页面数据
    $start = ($offset - 1) * $limit ;
    $allcount = sql_fetch_one_cell("select count(type_id) from user_favorite where `uid` = '$uid' and `type` = 3") ;

    $ret['data']['pagination'] = array() ;
    util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;

    $ret['data']['house'] = array() ;
    $bitem = sql_fetch_rows("select t.* , c.`name` , c.`city_id` from build_type t inner join bulid_city c on t.bid = c.`id` inner join user_favorite u on t.id = u.type_id where u.`uid` = '$uid' and u.`type` = 3 order by u.`addtime` desc , t.`id`  desc limit $start , $limit") ;
    foreach ($bitem as $b){
        $rn = array() ;
        util_set_FAVOURT_HOUSE($rn , $b) ;
        $ret['data']['house'][] = $rn ;
    }
    return $ret ;
}

// 排序后的城市列表
function sort_city_list($json , $session)
{
    $ret = array();
    $ret['data'] = array();

    $citylist = sql_fetch_rows("SELECT * FROM `grp_city` WHERE `search_index` IS NOT NULL AND `search_index` <> '' ORDER BY `search_index` ASC ") ;

    $ret['data']['sort_citys'] = array() ;
    foreach ($citylist as $c){
        $rn = array() ;
        util_set_CITY_SORT($rn , $c) ;

        $badd = false ;
        foreach ($ret['data']['sort_citys'] as &$sc){
            if ($sc['firstChar'] == $rn['firstChar']){
                $sc['citys'][] = $rn['citys'] ;
                $badd = true ;
            }
        }
        if ($badd == false){
            $newone = array() ;
            $newone['firstChar'] = $rn['firstChar'] ;
            $newone['citys'] = array() ;
            $newone['citys'][] = $rn['citys'] ;
            $ret['data']['sort_citys'][] = $newone ;
        }
    }
    return $ret ;
}

// 排序后的楼盘城市
function sort_building_list($json , $session)
{
    $ret = array();
    $ret['data'] = array();

    $city        = intval(get_jsonValue($json,"city")) ;    // 数值 | 城市编号

    $citylist = sql_fetch_rows("SELECT * FROM `bulid_city` WHERE `search_index` IS NOT NULL AND `search_index` <> '' AND `city_id` = '$city' ORDER BY `search_index` ASC ") ;
    $ret['data']['sort_buildings'] = array() ;
    foreach ($citylist as $c){
        $rn = array() ;
        util_set_BUILDING_SORT($rn , $c) ;

        $badd = false ;
        foreach ($ret['data']['sort_buildings'] as &$sc){
            if ($sc['firstChar'] == $rn['firstChar']){
                $sc['buildings'][] = array("id"=>$rn['buildings']['id'] , "name"=>$rn['buildings']['name']) ;
                $badd = true ;
            }
        }
        if ($badd == false){
            $newone = array() ;
            $newone['firstChar'] = $rn['firstChar'] ;
            $newone['buildings'] = array() ;
            $newone['buildings'][] = array("id"=>$rn['buildings']['id'] , "name"=>$rn['buildings']['name']) ;
            $ret['data']['sort_buildings'][] = $newone ;
        }
    }
    return $ret ;
}

// 搜索楼盘城市
function search_building($json , $session)
{
    $ret = array();
    $ret['data'] = array();

    $search_name = get_jsonValue($json,"search_name") ;    // 字符串 | 输入搜索楼盘字符串
    $city        = intval(get_jsonValue($json,"city")) ;            // 数值 | 城市编号

    $citylist = sql_fetch_rows("SELECT * FROM `bulid_city` WHERE `search_index` IS NOT NULL AND `search_index` <> '' AND `city_id` = '$city' AND `name` LIKE '%$search_name%' ORDER BY `search_index` ASC ") ;
    $ret['data']['buildings'] = array() ;
    foreach ($citylist as $c){
        $rn = array() ;
        util_set_BUILDING($rn , $c) ;
        $ret['data']['buildings'][] = $rn ;
    }
    return $ret ;
}


//  众筹list
function crowd_funding_list($json , $session)
{
    $uid = util_set_GETUID_for_SESSION($session) ;

    if ($uid <= 0){
        // 获取缓冲数据
        $cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
        $ret = mem_getValues($cache_key) ;
        if ($ret != null){
            return $ret ;
        }
    }

    $ok = false ;
    $usertype = intval(sql_fetch_one_cell("select `user_type` from user_info where `id` = '$uid'")) ;

    $ret = array();
    $ret['data'] = array();

    $state = intval(get_jsonValue($json,"state")) ;     // 众筹状态  -1 所有   ０即将开始，１正在众筹，２众筹成功结束，3 众筹失败结束 ， 4 成功状态已处理 ， 5 失败状态已处理'

    util_get_PAGINATION($json , $offset , $limit) ;
    if ($offset < 1)	$offset = 1 ;
    // 组织页面数据
    $start = ($offset - 1) * $limit ;

    $where = "" ;
    if ($state >= 0) {
        $where = "where `state` = $state and `useing` = 1" ;
    }else{
        $where = "where `useing` = 1" ;
    }
    $where .= " and (`user_types` is null or `user_types` = '' or `user_types` like '%$usertype%')" ;

    $allcount = sql_fetch_one_cell("select count(id) from crowd_item  $where ");

    $ret['data']['pagination'] = array() ;
    util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;

    $ret['data']['crowdList'] = array() ;
    $bitem = sql_fetch_rows("select * from crowd_item $where order by `id` desc limit $start , $limit") ;
    foreach ($bitem as $b){
        $rn = array() ;
        util_set_CROWD_FUNDING($rn , $b) ;
        $ret['data']['crowdList'][] = $rn ;
    }

    return $ret ;
}

// 众筹商品详情
function crowd_funding_detail($json , $session)
{
    $ret = array();
    $ret['data'] = array();

    $id = intval(get_jsonValue($json,"id")) ;

    $ret['data']['crowd_funding'] = array() ;
    $bitem = sql_fetch_rows("select * from crowd_price where `crowd_id` = '$id'") ;
    foreach ($bitem as $b) {
        $rn = array() ;
        util_set_CROWD_FUNDING_DETAIL($rn, $b);
        $ret['data']['crowd_funding'][] = $rn ;
    }
    return $ret ;
}


// 众筹轮播
function crowd_funding_list_broadcast($json , $session) // 众筹list轮播
{
    $ret = array();
    $ret['data'] = array();

    $ret['data']['broadcast'] = array() ;
    $bitem = sql_fetch_rows("select * from crowd_item where banner_flag = 1 order by `id` desc") ;
    foreach ($bitem as $b) {
        $rn = array() ;
        util_set_BROADCAST($rn, $b);
        $ret['data']['crowd_funding'][] = $rn ;
    }
    return $ret ;
}
/**
 * 根据订单号获取提现详情
 * @param   $json
 * @param   $session
 */
function rebate_order_details($json,$session){
    $trade_no = get_jsonValue($json,"trade_no") ;
    $uid = util_set_GETUID_for_SESSION($session) ;
    //查询个税税率  SELECT config_value FROM prf_config WHERE config_attribute='coupon_rate'
    $prf_config = sql_fetch_one("SELECT config_value FROM prf_config WHERE config_attribute='coupon_rate'") ;
    //个税 税率
    $prf_rate=$prf_config['config_value'];
    //根据 uid  trade_no 从 pay_trade_rebate      获取 rebate_money 返点金额  rebate_uid 返点uid
    $pay_trade_rebate=sql_fetch_one("select rebate_money,rebate_uid from pay_trade_rebate where uid={$uid} and trade_no={$trade_no}");
    $rebate_money=$pay_trade_rebate["rebate_money"];
    $rebate_uid=$pay_trade_rebate["rebate_uid"];
    //根据订单返点金额*税率 获取个税
    $rebate_rate=$rebate_money*($prf_rate/100);
    
    //根据   trade_no     从pay_trade              获取 number 件数   price_modify 订单金额
    $pay_trade=sql_fetch_one("select number,price_modify from pay_trade where trade_no={$trade_no}");
    $pay_trade_number=$pay_trade['number'];
    $price_modify=$pay_trade['price_modify'];
    //根据  rebate_uid   从user_info             获取昵称  电话号码/为空
    $userInfo=sql_fetch_one("select nickname,tel from user_info where id={$rebate_uid}");
    $nickname=$userInfo["nickname"];
    $tel=$userInfo["tel"];
    //根据 trade_no     从log_pay               获取endtime 结束时间   where state=2  取最后一条
    $log_pay=sql_fetch_one("select endtime from log_pay where state=2 and  trade_no ={$trade_no} order by starttime desc limit 1");
    $endtime=date("Y-m-d H:i:s",$log_pay["endtime"]); 
    
    //根据  user_id=rebate_uid   u_key=img_url_id    从user_info_ex          获取u_value
    $user_info_ex=sql_fetch_one("select u_value from user_info_ex where u_key='img_url_id' and user_id={$rebate_uid}");
    $headimg_id=$user_info_ex["u_value"];
    //根据 u_value  从sys_image               获取url
    $sys_image=sql_fetch_one("select url from sys_image where id={$headimg_id}");
    $avatar=$GLOBALS['URL_IMG_HEAD'].$sys_image["url"];
    //prf_rate 平台个税  rebate_money 返点金额   rebate_rate 返点税
    //price_modify 订单金额     pay_trade_number 商品个数  trade_no 订单编号
    //nickname 昵称  tel 电话   subsidy 平台补贴 0表示没有
    $available=$rebate_money-$rebate_rate+0;
    $rebateDetail=array("cashback"=>round($rebate_money,2),"tax"=>round($rebate_rate,2),
        "price"=>round($price_modify,2),"num"=>$pay_trade_number,"trade_no"=>$trade_no,"order_time"=>$endtime,
        "nickname"=>$nickname,"mobile"=>$tel,"subsidy"=>0,"status"=>3,"available"=>round($available,2)
    );
    $ret = array();
    $ret['rebateDetail'] = $rebateDetail;
    $ret['avatar']=$avatar;
    return $ret;
}

function test($json)
{
    // 获取缓冲数据
    $cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
    $ret = mem_getValues($cache_key) ;
    if ($ret != null){
        return $ret ;
    }
    
    util_get_PAGINATION($json , $offset , $limit) ;
    if ($offset < 1)	$offset = 1 ;
    // 组织页面数据
    $start = ($offset - 1) * $limit ;
    
    $addwhere = "" ;
    $filter = get_jsonValue($json,"filter") ;
    $room 	= get_jsonValue($filter , "room") ;			$addwhere .= _filter_room_set($room) ;		// 空间
    $style	= get_jsonValue($filter , "style") ;		$addwhere .= _filter_style_set($style) ;	// 风格
    $color 	= get_jsonValue($filter , "color") ;		$addwhere .= _filter_color_set($color) ;	// 色系
    $price	= get_jsonValue($filter , "price") ;		// 价格区间
    $price_min = get_jsonValue($price , "min") ;
    $price_max = get_jsonValue($price , "max") ;		$addwhere .= _filter_price_item(intval($price_min) , intval($price_max)) ;	// 价格区间
    $order_by = get_jsonValue($filter , "order_by") ;// 排序方式
    
    $ret = array() ;
    $ret['data'] = array() ;
    $ret['data']['set'] = array() ;
    
    $allcount = sql_fetch_one_cell("select count(sid) from taozhuang where `app_return` = 1 $addwhere") ;
    
    $ret['data']['pagination'] = array() ;
    util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
    
    $bitem = sql_fetch_rows("select * from taozhuang where `app_return` = 1 $addwhere order by `top_time` desc , `sid` desc limit $start , $limit") ;
    foreach ($bitem as $b){
        $rn = array() ;
        util_set_SET_SIMPLE($rn , $b) ;
        $ret['data']['set'][] = $rn ;
    }
    
    // 设置缓冲数据
    mem_setKey($cache_key, $ret) ;
    return $ret ;
}
/**
 * 查看vr列表
 */
function getAllVr($json, $session){
    
    // 获取缓冲数据
    $cache_key = md5($GLOBALS['PARAM_CONTENT_DATA']) ;
    $ret = mem_getValues($cache_key) ;
    if ($ret != null){
        return $ret ;
    }
    util_get_PAGINATION($json , $offset , $limit) ;
    if ($offset < 1)	$offset = 1 ;
    // 组织页面数据
    $start = ($offset - 1) * $limit ;
    $ret = array();
    $ret['data'] = array() ;
    $allcount = sql_fetch_one_cell("select count(id) from vr  ") ;
    $ret['data']['pagination'] = array() ;
    util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
    $vr_list= sql_fetch_rows("select * from vr  order by `t_order` desc,`id` desc  limit $start , $limit") ;
	foreach ($vr_list as $b){
        $rn = array() ;
        util_set_vr_list($rn , $b) ;
        $ret['data']['vr'][] = $rn ;
    }
    return $ret ;
}
/**
 * 查看vr详情
 */
function getOneVr($json , $session){
    $ret = array();
    $ret['data'] = array() ;
    $vr_id = get_jsonValue($json,"id") ;
    $vr_one=sql_fetch_one("select * from vr where id={$vr_id}");
    $vr_one["vr_url"]=$GLOBALS['URL_VR'].$vr_one["vr_url"]."/tour.html";
    $ret['data']['vr']=$vr_one;
    return $ret;
}

//不换装VR空间包含的单品
function vr_space_itemlist($json)
{
	$vr_id = get_jsonValue($json,"id") ;
	$vr_space_item_list = sql_fetch_rows("SELECT * FROM `vr_pos` inner join vr_sapce ON vr_pos.vr_space_id = vr_sapce.id where vr_pos.vr_id = $vr_id and vr_sapce.useing=1") ;
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['vr_itemlist'] = array() ;
	
	foreach($vr_space_item_list as $b){
		$name = $b['name'];
		$item_list = $b['item_id_list'];
		
		$addwhere = "" ;
		if (strlen($item_list) > 0){
			$addwhere .= " and `id` in ($item_list) order by `class_id` " ;
			
			$newone = array() ;
			$newone['item'] = array() ;
			$newone['vr_space_name'] = $name;
		
			$itemlist =  sql_fetch_rows("select * from sys_item where app_return = 1 $addwhere") ;
			foreach ($itemlist as $c){
				$rn = array() ;
				$rn['item']= array() ;
				util_set_ITEM_SIMPLE($rn, $c) ;	
				$newone['item'][] = $rn ;				
			}
			
            $ret['data']['vr_itemlist'][] = $newone ;
		}	
	}
	return $ret ;
}

// 判断用户以前是否是VIP用户
function get_designer_auditflag($json , $session)
{
	// 获取缓冲数据
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	$ret['auditflag'] = array() ;
	
	$ret['auditflag'] = 0 ;	
	if ($uid <= 0){
		return $ret ;
	}
	
	$designer = sql_fetch_one_cell("select audit_state from sys_user_designer where `uid` = $uid") ;
	if ($designer== 1){
		$ret['auditflag'] =1;
	}else{
		$ret['auditflag'] =0;
	}
	return $ret ;
}	

//用户注销
function user_logout($json,$session){
    // 获取缓冲数据
    $uid = util_set_GETUID_for_SESSION($session) ;
    $ret = array() ;
    $ret['logoutflag'] = array() ;
    
    $ret['logoutflag'] = 0 ;
    if ($uid <= 0){
        return $ret ;
    }
    $logout_result= sql_query("update  user_info SET `status`=2  WHERE id='$uid'  ") ;
    if ($logout_result== 1){
        $ret['logoutflag'] =1;
    }else{
        $ret['logoutflag'] =0;
    }
    return $ret ;
}



?>